Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Interesting. I had similar partial synthetic benchmark yesterday, thought to publish it mainly to advice against my "seek" solution as too slow, then decided not to :), because maybe it's not worth readers' effort.

Nevertheless, somewhat different results for a 1 million lines file, and fast NVMe SSD storage. Below is the case for returning a hash with chars counts, but it's similar for returning string.

$ perl vert2.pl ok 1 - same results ok 2 - same results ok 3 - same results (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) Rate seek buk substr slurp seek 0.920/s -- -61% -84% -88% buk 2.36/s 157% -- -58% -69% substr 5.66/s 515% 140% -- -26% slurp 7.69/s 736% 226% 36% -- 1..3

use strict; use warnings; use feature 'say'; use String::Random 'random_regex'; use Benchmark 'cmpthese'; use Test::More 'no_plan'; my $fn = 'dna.txt'; my $POS = 10; unless ( -e $fn ) { open my $fh, '>', $fn; print $fh random_regex( '[ACTG]{42}' ), "\n" for 1 .. 1e6; } is_deeply _seek(), _substr(), 'same results'; is_deeply slurp(), _substr(), 'same results'; is_deeply buk(), _substr(), 'same results'; cmpthese( 3, { substr => \&_substr, seek => \&_seek, buk => \&buk, slurp => \&slurp, }); sub slurp { open my $fh, '<', $fn; my $s = do { local $/ = undef; <$fh> }; my $count; $count-> { substr $s, $POS - 1 + 43 * $_, 1 }++ for 0 .. length( $s ) / 43 - 1; return $count } sub buk { open my $fh, '<', $fn; my $buf = chr( 0 ) x 43; my $ref = \substr( $buf, $POS - 1, 1 ); my $count; until ( eof $fh ) { substr( $buf, 0 ) = <$fh>; $count-> { $$ref }++ } return $count } sub _seek { open my $fh, '<', $fn; my $L = length( <$fh> ) - 1; seek $fh, $POS - 1, 0; my $count; until ( eof $fh ) { $count-> { getc $fh }++; seek $fh, $L, 1 } return $count } sub _substr { open my $fh, '<', $fn; my $count; $count-> { substr $_, $POS - 1, 1 }++ while <$fh>; return $count }

$ perl -v This is perl 5, version 26, subversion 0 (v5.26.0) built for x86_64-li +nux-thread-multi (with 1 registered patch, see perl -V for more detail)

In reply to Re^2: Faster and more efficient way to read a file vertically by vr
in thread Faster and more efficient way to read a file vertically by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-03-28 11:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found