in reply to code optimization
#!/usr/bin/perl use warnings; use strict; use Benchmark 'cmpthese'; sub arivu { my $input = shift; open my $FH, '<', $input or die; my $minimumAmount = 1e12; my ($amount, $weight); while (<$FH>) { if (/(\d+) (\d+)/) { if ($minimumAmount > ($1/$2)) { $minimumAmount = $1 / $2; $amount = $1; $weight = $2; } } } close $FH; return "$amount\t$weight\n"; } # arivu sub choroba { my $input = shift; open my $FH, '<', $input or die; my $minimumAmount = 1e12; my ($amount, $weight); while (<$FH>) { my ($a, $w) = split; if (defined $a and defined $w and (my $ratio = $a / $w) < $minimumAmount) { $minimumAmount = $ratio; $amount = $a; $weight = $w; } } close $FH; return "$amount\t$weight\n"; } # choroba my $input; $input .= (join ' ', map int(1 + rand 1000), 1 .. 2) . "\n" for 1 .. 1 +000; cmpthese(-1, {arivu => sub {arivu \$input}, choroba => sub {choroba \$input}, }); print arivu \$input; print choroba \$input; __END__ Output: Rate arivu choroba arivu 466/s -- -17% choroba 565/s 21% -- 8 953 8 953
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: code optimization
by JavaFan (Canon) on Nov 03, 2011 at 12:28 UTC | |
by choroba (Cardinal) on Nov 03, 2011 at 12:31 UTC |