substr will be faster though.
Yes, by several orders of magnitude...
use Benchmark qw(cmpthese);
use Math::BigFloat;
Math::BigFloat->precision(-2);
my $number = "999999999999999999999900";
cmpthese -1, {
substr => sub { my $num=$number; substr($num,-2,0) = '.'; },
BigFloat => sub { my $num=$number; Math::BigFloat->new($num)/100;
+},
};
__END__
Rate BigFloat substr
BigFloat 3794/s -- -100%
substr 1668189/s 43874% --
And that doesn't even involve accessing or printing out the constructed BigFloat again. |