Listing 2.
How fast is OO?
#!/usr/bin/perl -w # # Performance comparison for lots of calls to a small sub: # OO of varying degrees, static functions, and inlined. # use strict; use Benchmark; $Bogus::DATA = ""; my $COUNT = shift || 100000; printf "timing %d iterations; initializing...\n", $COUNT; for (my $i=0; $i < $COUNT; $i++) { vec($Bogus::DATA, $i, 8) = 42; } package Bogus; sub new { my $self = bless {}, shift; $self->{"data_ref"} = \$Bogus::DATA; return $self; } sub data_ref { return $_[0]->{"data_ref"}; } sub get_byte_superverbose_instance_data { my ($self, $where) = @_; return vec(${$self->data_ref()}, $where, 8); } sub get_byte_verbose_instance_data { my ($self, $where) = @_; return vec(${$self->{"data_ref"}}, $where, 8); } sub get_byte_terse_instance_data { return vec(${$_[0]->{"data_ref"}}, $_[1], 8); } sub get_byte_terse_static { return vec($Bogus::DATA, $_[1], 8); } sub get_byte_verbose_static { my ($self, $where) = @_; return vec($Bogus::DATA, $where, 8); } package main; sub get_byte_main_terse_static { return vec($Bogus::DATA, $_[0], 8); } sub get_byte_main_verbose_static { my ($where) = @_; return vec($Bogus::DATA, $where, 8); } $main::bogus = $main::bogus = new Bogus(); # shut up -w timethis($COUNT, '$x = $main::bogus->get_byte_superverbose_instance_data(0)', 'superverbose instance OO'); timethis($COUNT, '$x = $main::bogus->get_byte_verbose_instance_data(0)', 'verbose instance OO'); timethis($COUNT, '$x = $main::bogus->get_byte_terse_instance_data(0)', 'terse instance OO'); timethis($COUNT, '$x = $main::bogus->get_byte_verbose_static(0)', 'verbose static OO'); timethis($COUNT, '$x = $main::bogus->get_byte_terse_static(0)', 'terse static OO'); timethis($COUNT, '$x = main::get_byte_main_verbose_static(0)', 'verbose static call'); timethis($COUNT, '$x = main::get_byte_main_terse_static(0)', 'terse static call'); timethis($COUNT, '$x = vec($Bogus::DATA, 0, 8)', 'fully inlined');