Ok, lets give it a test -$n = scalar( split ',', $str ); print "$n\n";
And the comparison -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"; }
The benchmark result show that a simple split without making an anonymous copy is 20% faster.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% --
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |