LanX has asked for the wisdom of the Perl Monks concerning the following question:
inspired from the recent discussion I was seeking for the fastest way to count all fix substring in a long string.
But most approaches have the problem to allocate memory for a result list which is ignore anyway ...
DB<100> $l.="$_\t".(int rand 2)."\n" for 1..1e6 # linenumber TAB 0| +|1 RET DB<101> length $l => 8888896 DB<102> use Time::HiRes qw/time/ DB<103> sub timeit (&) { my $s=time; (shift)->(); print "<",time-$s.">\n"; } DB<104> $i=0; timeit { $i++ while ( $l =~ /\t1\n/g) }; print $i <0.454659938812256> 500338 DB<105> $i=0; timeit { $i = () = $l =~ /\t1\n/g }; print $i <1.18111300468445> 500338 DB<106> $i=0; timeit { $i = split /\t1\n/, $l }; print $i <0.507856845855713> 500339 DB<122> $i=0; timeit { local $/="\t1\n";open my $D,'<',\$l; $i++ whil +e (<$D>) }; print $i <0.703027963638306> 500339
Improvements welcome!
Cheers Rolf
( addicted to the Perl Programming Language)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Challenge: fastest count of substring
by BrowserUk (Patriarch) on May 06, 2013 at 16:15 UTC | |
|
Re: Challenge: fastest count of substring
by Limbic~Region (Chancellor) on May 06, 2013 at 15:40 UTC | |
|
Re: Challenge: fastest count of substring
by hdb (Monsignor) on May 06, 2013 at 20:46 UTC | |
by LanX (Saint) on May 06, 2013 at 21:06 UTC |