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
In reply to Re: Re: Chomp Maybe?
by hardburn
in thread Chomp Maybe?
by th3monk3y
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |