[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Omaha.pm] Benchmarking new versions of a module



So we re-wrote a module today for the sake of speed. Twice. Did we make it faster or slower? How much faster or slower?

Quick temp directory and "use Benchmark;"...

j



$ ls -al
total 53
drwxr-xr-x    2 jhannah  users         168 2006-06-05 15:14 .
drwxrwxrwx    5 pbaker   resmis       1024 2006-06-05 15:13 ..
-rw-r--r--    1 jhannah  users        1479 2006-06-05 15:07 j.pl
-rwxr-xr-x    1 jhannah  users       13886 2006-06-05 14:54 RateRule_new2.pm
-rwxr-xr-x    1 jhannah  users       13748 2006-06-05 14:53 RateRule_new.pm
-rwxr-xr-x    1 jhannah  users       12760 2006-06-05 14:44 RateRule_old.pm


$ cat j.pl
use strict;
use RateRule_old;
use RateRule_new;
use RateRule_new2;
use Benchmark;

timethese(10000, {
  'old' => sub {
      my $obj  = Model::omares::Complex::Rates::RateRule_old->new();
      set_get($obj);
  },
  'new' => sub {
      my $obj  = Model::omares::Complex::Rates::RateRule_new->new();
      set_get($obj);
  },
  'new2' => sub {
      my $obj  = Model::omares::Complex::Rates::RateRule_new2->new();
      set_get($obj);
  }
});


sub set_get {
  my ($obj) = @_;

my @fields = qw( date
     discount valid_arrival valid_days advance_purchase min_night_stay max_night_stay
     min_adults max_adults max_persons free_sale accepted_len_stay active_status discount
     how_disc extra_money how_apply_money package_code allotment commission commission_amt
     max_advance min_advance
  );
  my %values = qw(
     commission Y
     date       06/05/2006
  );
  my $field;
  foreach $field (@fields) {
     my $get = "get_$field";
     my $set = "set_$field";
     #print "$field...\n";
     my $value = $values{$field} || 100;
     $obj->$set($value);
     my $check = $obj->$get;
     die "[$field] failed on obj $obj ($check ne $value)" unless ($check eq $value);
  }

}


$ perl j.pl
Benchmark: timing 10000 iterations of new, new2, old...
      new:  5 wallclock secs ( 5.44 usr +  0.00 sys =  5.44 CPU) @ 1838.24/s (n=10000)
     new2:  2 wallclock secs ( 2.05 usr +  0.00 sys =  2.05 CPU) @ 4878.05/s (n=10000)
      old: 18 wallclock secs (17.61 usr +  0.01 sys = 17.62 CPU) @ 567.54/s (n=10000)