in reply to Preferred Methods

There is nothing wrong with your approach at all - Another method might be to employ the sprintf operator for which there is an excellent tutorial on this site. eg.

sub roundThatBadDog { my $seconds = shift; return sprintf("%d", $seconds); }

Or alternatively, if you are wanting precision, replace the %d with %.xf where x is a integer value representing the order of decimal precision desired.

 

Update

 

In response to japhy's comments, I checked out the FAQ and ran a few timing tests to compare.

From my reading, the comments in the FAQ hardly suggest the avoidance of sprintf function for rounding purposes but rather the use of a more intrinsic, known method of rounding (such as those submitted by japhy here) for sensitive or high-value operations. The example given is a financial environment where the FAQ rightly suggests that a programmer should implement their own rounding code rather than relying on system functions.

With regard to a timing comparison, the following is the output - Make of it what you will :-)

Benchmark: timing 100000 iterations of japhy, rob_au... japhy: 3 wallclock secs ( 4.13 usr + 0.00 sys = 4.13 CPU) @ 24 +213.08/s (n=100000) rob_au: 4 wallclock secs ( 4.23 usr + 0.00 sys = 4.23 CPU) @ 23 +640.66/s (n=100000)

 

Update

 

These results may not be truly indicative of function performance, and may I add, not intendedly so. japhy discusses performance differences in his more comprehensive comparison here. ++japhy!

 

And the (now superceded) code used for the benchmark timings ...

#!/usr/bin/perl use Benchmark; use strict; timethese(100000, { 'rob_au' => q! my @values = ( 1.24, 5.43, -98.54, -73.667, 0.67, 2.34, 76.89, + -999.99, 34.52, 67.89 ); foreach my $value (@values) { my $result = sprintf("%d", $value); } !, 'japhy' => q! my @values = ( 1.24, 5.43, -98.54, -73.667, 0.67, 2.34, 76.89, + -999.99, 34.52, 67.89 ); foreach my $value (@values) { my $result = int($value * 10**0 + .5 * ($value < 0 ? -1 : +1)) / 10**0; } ! });
perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Replies are listed 'Best First'.
Re: Re: Preferred Methods
by japhy (Canon) on Jan 14, 2002 at 11:33 UTC
    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:??;

Re: Re: Preferred Methods
by japhy (Canon) on Jan 14, 2002 at 09:27 UTC
    Using sprintf() for rounding in a value-sensitive environment is warned against in the FAQ itself. Not to mention mathematical rounding functions are far faster.

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