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??
Why make another copy of the array with @{[ ... ]}? I would rather do this instead -
$n = scalar( split ',', $str ); print "$n\n";
Ok, lets give it a test -
use strict; use Benchmark qw/timethese cmpthese/; my $str = join ',',map{('a'..'z')[rand 26] x (1 + rand 5)}0..1000; cmpthese( timethese(10000, { 'split_anonymous' => '&split_anonymous', 'split_simple' => '&split_simple', }) ); sub split_anonymous { my $n = @{[split ',', $str]}; #print "$n\n"; } sub split_simple { my $n = scalar split ',', $str; #print "$n\n"; }
And the comparison -
Benchmark: timing 10000 iterations of split_anonymous, split_simple... split_anonymous: 19 wallclock secs (19.05 CPU) @ 525.02/s (n=10000) split_simple: 16 wallclock secs (15.77 CPU) @ 634.32/s (n=10000) Rate split_anonymous split_simple split_anonymous 525/s -- -17% split_simple 634/s 21% --
The benchmark result show that a simple split without making an anonymous copy is 20% faster.


In reply to Re: Re: How to get the number of fields found by split without a warning? by Roger
in thread How to get the number of fields found by split without a warning? by Rudif

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 admiring the Monastery: (5)
As of 2024-04-18 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found