Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Fastest split possible

by marioroy (Prior)
on Nov 08, 2019 at 03:22 UTC ( [id://11108468]=note: print w/replies, xml ) Need Help??


in reply to Re: Fastest split possible
in thread Fastest split possible

Update 1: Added walk2 by pushing to stack.
Update 2: The string length now obtained inside C.
Update 3: Changed from for loop to while loop.

Ah, I tried Inline::C. Walk1 runs faster than split in older Perl <= 5.20.x.

... use Inline 'C' => <<'END_C'; #include <stdlib.h> #include <string.h> SV * c_walk1(SV *sv) { STRLEN len; const char *s = SvPV_const(sv, len); const char *m = s, *strend = s + len; AV *ret = newAV(); while (s < strend) { if (*s++ == '\n') { av_push(ret, newSVpvn(m, s - m)); m = s; } } return newRV_noinc((SV *) ret); } void c_walk2(SV *sv) { STRLEN len; const char *s = SvPV_const(sv, len); const char *m = s, *strend = s + len; Inline_Stack_Vars; Inline_Stack_Reset; while (s < strend) { if (*s++ == '\n') { Inline_Stack_Push(sv_2mortal(newSVpvn(m, s - m))); m = s; } } Inline_Stack_Done; } END_C # walk1 - receive array ref { my $string = $huge_string; my $start = time; my $lines = c_walk1($string); printf "duration walk1: %0.3f seconds\n", time - $start; printf "%d lines\n\n", scalar(@$lines); } # walk2 - receive list { my $string = $huge_string; my $start = time; my @lines = c_walk2($string); printf "duration walk2: %0.3f seconds\n", time - $start; printf "%d lines\n\n", scalar(@lines); }

Output from Perl 5.20.3 and Perl 5.26.1

$ /opt/perl-5.20.3/bin/perl demo.pl duration memfh: 0.526 seconds 917504 lines duration regex: 0.448 seconds 917504 lines duration split: 0.110 seconds 917504 lines duration trim : 0.268 seconds 917504 lines duration walk1: 0.073 seconds 917504 lines duration walk2: 0.110 seconds 917504 lines
$ /opt/perl-5.26.1/bin/perl demo.pl duration memfh: 0.479 seconds 917504 lines duration regex: 0.442 seconds 917504 lines duration split: 0.066 seconds 917504 lines duration trim : 0.259 seconds 917504 lines duration walk1: 0.070 seconds 917504 lines duration walk2: 0.084 seconds 917504 lines

Regards, Mario

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11108468]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-18 06:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found