#!/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.52); 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)) } }, }); #### 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 japhy_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% --