Hi
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)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|