Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^4: Performance, Abstraction and HOP

by Anonymous Monk
on Sep 09, 2005 at 16:14 UTC ( #490626=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Performance, Abstraction and HOP
in thread Performance, Abstraction and HOP

So if Perl's function calls are "too slow" (whatever that means, and nobody in this thread has suggested any relevant meaning)
~/perl$ cat func_test.pl #!/usr/bin/perl #sum of number between 1 and 1,000,000 $n = 1_000_000; print addup($n, 0); sub addup { return $_[1] if !$_[0]; return addup($_[0]-1,$_[1]+$_[0]); } ~/perl$ cat loop_test.pl #!/usr/bin/perl #sum of number between 1 and 1,000,000 $n = 1_000_000; $sum = 0; $sum += $n-- while($n); ~/perl$ time ./func_test.pl 500000500000 real 0m4.025s user 0m3.568s sys 0m0.455s ~/perl$ time loop_test.pl real 0m0.390s user 0m0.388s sys 0m0.001s


Comment on Re^4: Performance, Abstraction and HOP
Download Code
Replies are listed 'Best First'.
Re^5: Performance, Abstraction and HOP
by runrig (Abbot) on Sep 14, 2005 at 22:21 UTC
    I'm not sure that a memory hogging recursive function makes for a fair benchmark of function calling speed (even if the tail recursion could be optimized away in another language -- and if the tail recursion could be optimized away, you wouldn't be calling the function anymore, you'd just be doing a goto). I think this version of func_test.pl is more fair (which attempts to test the speed of function calling rather than memory allocation):
    #!/usr/bin/perl #sum of number between 1 and 1,000,000 $n = 1_000_000; my $t = 0; $t = addup( $t, $n-- ) while $n; print "$t\n"; sub addup { $_[0] + $_[1]; }
    and then the two come out closer:
    $ time ./func_tst.pl 500000500000 real 0m5.68s user 0m5.48s sys 0m0.02s $ time ./loop_test.pl 500000500000 real 0m1.48s user 0m1.47s sys 0m0.01s

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2015-07-30 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (270 votes), past polls