Re: How to use a textfile for filenames??



Dan wrote:
I would like to take each line of the file (one URL per line) and
remove the Http://


Use the substitution operator ( s/// ) for that. The s/// operator
is documented in:

Would using substr() be an even more efficient method of doing this?

According to the following benchmark, it depends on which substr() form
you use:

#!/usr/bin/perl
use strict;
use warnings;

use Benchmark qw/cmpthese/;

sub gen_lines {
qw{
Http://www.yahoo.com
Http://gmail.google.com
Http://disney.go.com
Http://www.perldoc.com
Http://search.cpan.org
Http://www.oreilly.com
Http://www.ge.com
Http://www.stratus.com
Http://www.es11.com
Http://www.bankofamerica.com
};
}

cmpthese(-10, {
'substr_3arg' => sub {
my @lines = gen_lines();
substr($_, 0, 7) = q{} for @lines;
},
'substr_4arg' => sub {
my @lines = gen_lines();
substr($_, 0, 7, q{}) for @lines;
},
's///' => sub {
my @lines = gen_lines();
s{^Http://}{} for @lines;
}
}
);

__END__
Benchmark: running s///, substr_3arg, substr_4arg, each for at least 10
CPU seconds...
s///: 10 wallclock secs (10.52 usr + 0.01 sys = 10.53 CPU) @
10956.03/s (n=115367)
substr_3arg: 11 wallclock secs (10.22 usr + 0.01 sys = 10.23 CPU) @
10105.96/s (n=103384)
substr_4arg: 10 wallclock secs (10.54 usr + 0.00 sys = 10.54 CPU) @
12353.89/s (n=130210)
Rate substr_3arg s/// substr_4arg
substr_3arg 10106/s -- -8% -18%
s/// 10956/s 8% -- -11%
substr_4arg 12354/s 22% 13% --


(If I've done something that renders this test invalid, I'd appreciate
hearing about it... I'm rather a novice when it comes to Benchmarking)

Paul Lalli

.



Relevant Pages

  • Re: script to create to create tar and place files in the appropriate folder
    ... JWK> Uri Guttman wrote: ... >> that is called 4 arg substr and it is way underused in perl. ... >> benchmark shows the significant speedup of about 2x: ... what i see is that the extra assignment to $z is making that the largest ...
    (perl.beginners)
  • Re: How to use a textfile for filenames??
    ... According to the following benchmark, it depends on which substr() form ... sub gen_lines { ... "Reply" at the bottom of the article headers. ...
    (comp.lang.perl.misc)
  • Re: How to find regex at specific location on line
    ... > I believe the conventional way to do a regex search when you ... substr() solution nor a pure regex solution so I just kind of skipped ... Benchmark: timing 1000000 iterations of plain_regex, plain_substr, ...
    (perl.beginners)
  • Re: How to use a textfile for filenames??
    ... Would using substr() be an even more efficient method of doing this? ... According to the following benchmark, ... hence speeding up the substitution would be wasted time. ...
    (comp.lang.perl.misc)
  • Re: How to use a textfile for filenames??
    ... Sinan Unur wrote: ... If I were to use substr here, ... benchmark with all four methods.... ... sub gen_lines { ...
    (comp.lang.perl.misc)