Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: Counting the number of items returned by split without using a named array

by blazar (Canon)
on May 03, 2006 at 09:42 UTC ( #547151=note: print w/replies, xml ) Need Help??


in reply to Re^2: Counting the number of items returned by split without using a named array
in thread Counting the number of items returned by split without using a named array

Just out of curiosity (it doesnt really matter in my case): which one would be the more (CPU- and memory-) efficient one?

It hardly ever matters. As a wild guess I would say that since the former involves doing something and then undoing it and that something is taking a reference, it is more computationally intensive. In case of doubt

use Benchmark;

I may well (and happily!) prove wrong...

  • Comment on Re^3: Counting the number of items returned by split without using a named array
  • Download Code

Replies are listed 'Best First'.
Re^4: Counting the number of items returned by split without using a named array
by Anonymous Monk on May 03, 2006 at 11:02 UTC
    As you wish (or not):
    $ cat foo.pl ; echo "--------";echo; ./foo.pl #!/usr/bin/perl ################## use Benchmark qw(:all) ; sub test1(){ $entries = @{[ split ]}; } sub test2(){ $entries = () = /\S+/g; } $_="This is an example string with several words bla bla bla\n"; $count=1E+7; timethis ($count, "test1()"); print "------------\n"; timethis ($count, "test2()"); -------- timethis 10000000: 13 wallclock secs (12.37 usr + 0.01 sys = 12.38 CP +U) @ 807754.44/s (n=10000000) ------------ timethis 10000000: 7 wallclock secs ( 6.66 usr + 0.00 sys = 6.66 CP +U) @ 1501501.50/s (n=10000000)

      Adding use warnings; results in

      Use of uninitialized value in split at foo.pl line 9.

      Hmmm... adding (-l and) print $entries to both subs results in:

      Use of uninitialized value in split at foo.pl line 9. 0 Use of uninitialized value in pattern match (m//) at foo.pl line 14. 0

      Now,

      #!/usr/bin/perl use strict; use warnings; use Benchmark qw(:all :hireswallclock); my $str="This is an example string with several words bla bla bla"; sub test1 () { local $_=$str; my $entries = @{[ split ]}; } sub test2 () { local $_=$str; my $entries = () = /\S+/g; } cmpthese -10, { deref => \&test1, goatse => \&test2, }; __END__

      results in

      Rate goatse deref goatse 16325/s -- -42% deref 28017/s 72% --

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2021-01-23 10:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?