in reply to Re: Preferred Methods
in thread Preferred Methods

Your benchmark is misleading, since you are comparing my rounding function (made in-place) with sprintf "%d", which does not round -- it is the equivalent (albeit a slower one) of int(), which truncates, not rounds.

Restructuring the benchmark, I find vastly different results:

#!/usr/bin/perl use Benchmark 'cmpthese'; use strict; my @n = (1.24, 5.43, -98.54, -73.667, 0.67, 2.34, 76.89, -999.99, 34.5 +2); sub round { my ($n, $p) = @_; $p ||= 0; int($n * 10**$p + .5 * ($n < 0 ? -1 : 1)) / 10**$p; } cmpthese(-5, { rob_int => sub { for (@n) { my $x = sprintf "%d", $_ } }, rob_round => sub { for (@n) { my $x = sprintf "%.0f", $_ } }, japhy_int => sub { for (@n) { my $x = int $_ } }, japhy_round => sub { for (@n) { my $x = round($_,0) } }, japhy_inplace => sub { for (@n) { my $x = int($_ + .5 * ($_ < 0 ? -1 : 1)) } }, });
The results are thus:
Benchmark: running japhy_int, japhy_round, japhy_inplace, rob_int, rob_round for at least 5 CPU seconds... japhy_int: 20326.40/s (n=108543) japhy_round: 3272.93/s (n= 17412) japhy_inplace: 11470.71/s (n= 61483) rob_int: 11797.18/s (n= 62643) rob_round: 1964.03/s (n= 10429) Rate rob_round japhy_round japhy_inplace rob_int japh +y_int rob_round 1964/s -- -40% -83% -83% + -90% japhy_round 3273/s 67% -- -71% -72% + -84% japhy_inplace 11471/s 484% 250% -- -3% + -44% rob_int 11797/s 501% 260% 3% -- + -42% japhy_int 20326/s 935% 521% 77% 72% + --
What this all means is that, when it comes to truncating, it is about 75% faster to use int() and it is to use sprintf("%d"). It also shows that my function is 67% faster than using sprintf("%.0f") (which, I remind you, returns IEEE values which may not be appropriate for your computations). It also shows that if you were to inline my function call, you would run 250% faster -- sigh, the overhead of function calls.

This data also supports the previous finding, that my function, made in-place, runs about as fast as sprintf("%d").

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;