To Object Orient or not to Object Orient, that is the question.
Whether 'tis nobler of mind to suffer the subs and gotos of outrageous C,
Or to use Classes against a sea of spaghetti.

(apologies to Shakespear)

Try this:

# Compare OO construction against non-OO.
# $0 oo will run the OO version
# $0 bless will run the blessing version
# $0 with no params the non-OO
use strict;

use Benchmark ':hireswallclock';

{
    package ObjectAddress;
    
    sub new {
   my ($class, %opts) = @_;
   return bless(\%opts, $class);
    };
}

my $make;
my $what;
if (scalar(@ARGV) && $ARGV[0] eq 'oo') {
    $what = "OO";

    $make = sub {
   my $i = shift;
   return ObjectAddress->new(
       webs => ['rootweb', 'web', 'subweb', 'subsubweb'],
       topic => 'Topic',
       rev => 123,
       attachment => 'filename',
       i => $i);
    }
} elsif (scalar(@ARGV) && $ARGV[0] eq 'bless') {
    $what = "Bless";
    $make = sub {
   my $i = shift;
   return bless({
       webs => ['rootweb', 'web', 'subweb', 'subsubweb'],
       topic => 'Topic',
       rev => 123,
       attachment => 'filename',
       i => $i
   },"ObjectAddress")
    }
} else {
    $what = "No OO";
    $make = sub {
   my $i = shift;
   return {
       webs => ['rootweb', 'web', 'subweb', 'subsubweb'],
       topic => 'Topic',
       rev => 123,
       attachment => 'filename',
       i => $i
   }
    }
};

my $t0 = Benchmark->new();
my @objects;
for my $i (1..100000) {
    $objects[$i] = &$make();
}
my $t1 = Benchmark->new;
my $td = timediff($t1, $t0);
print "$what took:",timestr($td),"\n";

-- CrawfordCurrie - 09 Feb 2011

Running this gives me very different results each time, dependent on other processes on the machine. But overall it looks like OO costs about 10% to 20% overhead.

-- ArthurClemens - 09 Feb 2011
Topic revision: r3 - 09 Feb 2011, CrawfordCurrie
 
The copyright of the content on this website is held by the contributing authors, except where stated elsewhere. see CopyrightStatement. Creative Commons License