My first thought was, 'Silly guy, why is he writing C code? Doesn't he know the Perl(tm) way?'.
Then I thought, sprintf() is expensive .... how fast is it to run the integer math?
use strict; use warnings; use diagnostics; use Benchmark qw( :all );; my ( $number, $num ); $number = 1100.64686081907; cmpthese( -1, { sprintf => sub { $num = sprintf "%.2f", $number; }, round => sub { $num = int( $number * 100 + .5 ) / 10 +0; }, } ); # produced the results ... Rate sprintf round sprintf 123242/s -- -86% round 851644/s 591% --
So integer math rounding is 6 times as fast as using sprintf.
Of course, if you're doing less than 50,000, you may not notice the time difference, especially if you blink.
--
TTTATCGGTCGTTATATAGATGTTTGCA
In reply to Blushing at the facts
by TomDLux
in thread Simple Rounding
by einerwitzen
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |