hi,
For a long time I was wondering what is the speed difference, between obj vs func, by_name vs positional parameter passing.
And some cases is it worth it to choose the faster way over the more secure and convinient
So here are the results.
Rate obj_named_param named_pram obj_pos_param obj_i +nternal param obj_named_param 392157/s -- -14% -45% + -48% -56% named_pram 454545/s 16% -- -37% + -40% -49% obj_pos_param 716332/s 83% 58% -- + -5% -19% obj_internal 754148/s 92% 66% 5% + -- -15% param 889680/s 127% 96% 24% + 18% -- localhost work # perl time.pl Rate obj_named_param named_pram obj_pos_param obj_ +internal param obj_named_param 387297/s -- -12% -45% + -48% -55% named_pram 440141/s 14% -- -37% + -41% -49% obj_pos_param 701262/s 81% 59% -- + -6% -19% obj_internal 742942/s 92% 69% 6% + -- -14% param 860585/s 122% 96% 23% + 16% --
The fastest way as probably was expected was using subroutines and passing the parameters by position. The interesting thing is that it is more than 2 times faster compared to using object and passing the values by name. That seems like alot ;(, I mean there is many modules which use named params, if we can speed this on CPAN level, it will be many saved cpu-cycles ;).
Also it seems that the time consumer here is converting the @_ => %p.
param - sub by position named_param - sub by name obj_internal - the values we use are stored inside the object. obj_pos_param - obj passing values by position obj_named_param - obj passed by name
Here is the test script :
#!/usr/bin/perl package Blah; sub new { my $class = shift; my $self = { arg1 => 1, arg2 => 1 }; bless $self,$class ; return $self; } sub obj_internal { my $self = shift; return $$obj{arg1} + $$obj{arg2} } sub obj_pos_param { my ($self, $p1,$p2) = @_; return $p1 + $p2 } sub obj_named_param { my ($self, %p) = @_; return $p{arg1} + $p{arg2} } package main; use Benchmark qw(:all); our $obj = Blah->new; sub param { my ($p1,$p2) = @_; return $p1 + $p2 } sub named_param { my %p = @_; return $p{arg1} + $p{arg2} } cmpthese (5000000, { param => 'param(1,1)', named_pram => 'named_param(arg1 => 1,arg2 => 1)', obj_internal => '$obj->obj_internal()', obj_pos_param => '$obj->obj_pos_param(1,1)', obj_named_param => '$obj->obj_named_param(arg1 => 1,arg2 => 1)', })
Correct me if my micro benchmark are flawed.(i mean i know they are flawed if you look at the big picture, but in this micro way..).
Do you know some trick that we can use to speed up by_name passing ?

In reply to speed : obj vs func, by_name vs positional by rootcho

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.