I really encourage you to Benchmark instead of assuming. You will get surprises somewhat often. And assuming a simple arithmetic op is a performance killer seems silly. It took 10 million iterations to even be able to get this to benchmark without complaining it wasn't enough to be sure; and while a fifteenth of a millionth of a second is indeed three times faster than a fifth of a millionth of a second… does it actually matter on any level for the application in question?
moo@cow[483]~>perl pm-dice
use strictures;
use Benchmark "cmpthese";
sub d10 {
my $self = shift;
return rand(10);
}
sub d10_2 { rand(10) }
sub d10_3 { 1 + int( rand(10) ) }
sub d10_4 { 1 + rand(10) }
cmpthese( 10_000_000, {
orig => \&d10,
orig_simple => \&d10_2,
int_plus_one => \&d10_3,
plus_one => \&d10_4,
});
__END__
Rate orig int_plus_one plus_one orig_si
+mple
orig 4048583/s -- -19% -38%
+ -74%
int_plus_one 5000000/s 24% -- -24%
+ -68%
plus_one 6578947/s 62% 32% --
+ -59%
orig_simple 15873016/s 292% 217% 141%
+ --
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|