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:??;


In reply to Re: Re: Preferred Methods by japhy
in thread Preferred Methods by vek

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.