Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
update:
I seem to be getting different times on each run (and different winners too), but over the whole split does appear to be the winner. I've changed the regex to a more condensed version.

#!/usr/bin/perl -w use Benchmark 'timethese'; use strict; print "split: ", use_split(), "\n"; print "regex: ", use_regex(), "\n"; sub use_split { $_ = " one two three four five six"; split; # See Jouke's post (this may be faster). This is dep +recated though. return $_[3]; } sub use_regex { $_ = " one two three four five six"; s/^\s+//; m/^(?:[^\s]+\s+){3}([^\s]+)/; return $1; } timethese(-10, { regex => \&use_regex, split => \&use_split }); timethese(-50, { regex => \&use_regex, split => \&use_split }); timethese(-100, { regex => \&use_regex, split => \&use_split });

The results (updated to 10, 50 and 100) (...drumroll please...):
Benchmark: running regex, split, each for at least 10 CPU seconds... regex: 12 wallclock secs (10.64 usr + 0.00 sys = 10.64 CPU) @ 40 +522.18/s (n=431156) split: 10 wallclock secs (10.02 usr + 0.00 sys = 10.02 CPU) @ 37 +290.82/s (n=373654)
and:
Benchmark: running regex, split, each for at least 50 CPU seconds... regex: 68 wallclock secs (54.11 usr + 0.13 sys = 54.24 CPU) @ 40 +957.93/s (n=2221558) split: 56 wallclock secs (49.95 usr + 0.05 sys = 50.00 CPU) @ 38 +128.48/s (n=1906424)
and
Benchmark: running regex, split, each for at least 100 CPU seconds... regex: 136 wallclock secs (110.38 usr + 0.32 sys = 110.70 CPU) @ + 40548.93/s (n=4488766) split: 112 wallclock secs (100.00 usr + 0.02 sys = 100.02 CPU) @ + 38995.86/s (n=3900366)

[~]


In reply to (ar0n: split/regex benchmark) RE (2): novice by ar0n
in thread novice by mrt

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 examining the Monastery: (5)
As of 2024-04-19 07:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found