Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: new keyword "size" to avoid scalar trap ? ( scalar @array != scalar(LIST) )

by LanX (Chancellor)
on Dec 07, 2012 at 17:28 UTC ( #1007802=note: print w/replies, xml ) Need Help??


in reply to Re: new keyword "size" to avoid scalar trap ? ( scalar @array != scalar(LIST) )
in thread new keyword "size" to avoid scalar trap ? ( scalar @array != scalar(LIST) )

I care less about the name than the functionality, could be also "count" or whatever suits.

I suppose "elems" is already known from ruby or perl6? So why not.

But tobyinks and your suggestion w/o prototypes is considerably slower on large arrays.

DB<106> use Time::HiRes qw(time) DB<107> sub elems { scalar @_ } DB<108> @a=(1..1e6);0 DB<109> $t=time; $count = scalar @a; print time-$t 7.10487365722656e-05 DB<110> $t=time; $count = elems @a; print time-$t 0.0554749965667725

Perl 5.10!

Cheers Rolf

) Well the plural in "elems" somehow indicates a list to be returned.

) as already said

  • Comment on Re^2: new keyword "size" to avoid scalar trap ? ( scalar @array != scalar(LIST) )
  • Download Code

Replies are listed 'Best First'.
Re^3: new keyword "size" to avoid scalar trap ? ( scalar @array != scalar(LIST) )
by Laurent_R (Abbot) on Dec 08, 2012 at 15:46 UTC

    It is most probably the copying of the argument list (the full array) in the function call that takes this time difference.

    Passing a reference to the array would probably solve the issue, but the syntax would become less obvious to use.

      No copying takes place :) however the same amount of scalars is aliased ;)
      Thats why we need prototypes or a special C-implementaton.

      according to the perlsub sub size (+;@) { } could be used, to call

       size @array

      or

       size 1,2,3

      but since I'm using 5.10, I can't test it...

      Cheers Rolf

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007802]
help
Chatterbox?
[afoken]: http://floating- point-gui.de/ ?
[haukex]: What Every Computer Scientist Should Know About Floating-Point Arithmetic?
[Discipulus]: the winner is..
[Discipulus]: Mr haukex from Germany! thanks a lot to the other parteciapant our dear Mr afoken.. ;=)
[Discipulus]: no, seriously thanks both, i missed 'scientist' and 'floating point' and the resarche was imposible..
Discipulus go to his cell to bookmark this
[haukex]: Well to be honest I just did a super search for "every should know floating" ;-)

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2017-06-26 20:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (594 votes). Check out past polls.