I have been messing around with optimizing code for performance and been benchmarking a few mthods and recording the difference in speed of execution.
The following code I wrote as a simple test to compare the difference between the 2 "for loop" methods of syntax.
#!/usr/bin/perl -w
use Benchmark;
use strict;
my $results = timethese( 30, {
a => sub{my $total = 0; $total += $_ for(1..1e6);},
b => sub{my $total = 0; for(1..1e6){$total += $_;}},
} );
Here are the results returned when run:
Benchmark: timing 30 iterations of a, b...
a: 8 wallclock secs ( 8.30 usr + 0.00 sys = 8.30 CPU) @ 3
+.62/s (n=30)
b: 9 wallclock secs ( 9.03 usr + 0.02 sys = 9.05 CPU) @ 3
+.32/s (n=30)
Does anyone have any idea why the method is subroutine two() is slower in execution than the method in subroutine one()?
The recorded difference might not seem like much but the larger the array context is the larger the delta between the two becomes. Try changing the "1e6" to "1e7" and you can see what I am talking about.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.