in reply to Re: Chomp Maybe?
in thread Chomp Maybe?

Actually, I think it would be better to just get the absolute value:

$balance_due = abs($balance_due);

This protects you in the event that $balance_due is a positive number (which shouldn't happen according to the SQL, but I've always beleived that a good programmer is someone who looks both ways on a one way street). It's also close enough in speed to negation that random noise is a factor in execution time. Code:

#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); my $data = -12345; my $count = pop || 500000; cmpthese($count, { negate => sub { my $i = -$data }, absolute => sub { my $i = abs($data) }, regex => sub { my ($i) = $data =~ s/-// }, });

Results:

Benchmark: timing 500000 iterations of absolute, negate, regex... absolute: 1 wallclock secs ( 1.24 usr + 0.00 sys = 1.24 CPU) @ 40 +4858.30/s (n=500000) negate: 1 wallclock secs ( 1.15 usr + 0.00 sys = 1.15 CPU) @ 43 +3275.56/s (n=500000) regex: 3 wallclock secs ( 3.16 usr + 0.00 sys = 3.16 CPU) @ 15 +8328.06/s (n=500000) Rate regex absolute negate regex 158328/s -- -61% -63% absolute 404858/s 156% -- -7% negate 433276/s 174% 7% --

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Re: Re: Re: Chomp Maybe?
by The Mad Hatter (Priest) on Jul 01, 2003 at 18:32 UTC
    ++, you're absolutely right. When writing up my node, I forgot all about just getting the absolute value. ; )