The Speed of Moose

So now that that I have three prototypes out there for DA


  1. Pure Moose

  2. MooseX::Scaffold and

  3. MooseX::ShortCut::BuildInstance


The question becomes which one to to choose. They all do what I want them to enforce my API , keep the LSD code from beading into the DA code and all allow me now enter just a connection object and the correct LSD will be loaded.

One thing to do is run some benchmark test to see how fast the various scripts load and then run. I might not matter at this early stage bu I could see that the LSDs could become rather extensive as my original one was something like 2000+ lines of code and that could take some time to load.

I know it is rather old school of me to look into this as given the speed of modern system instaniating my object would likely be one of the quicker part of the code and the actual query would be the long part of this code, but every little bit helps.

So lets add in that good old perl Benchmark into my test code and see what I get. Using Benchmark is quite easy just add this


use Benchmark;
my $bm_s = Benchmark->new;

at the start then these lines at the end

my $bm_e = Benchmark->new;
my $diff = timediff($bm_e,$bm_s );
print "the code took:",timestr($diff),"\n";

and for the Pure Moose I get

the code took: 0 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU)

opps thats not right. I think we are going a little too quick so up the anti by using ':hireswallclock' on the use like this


  use Benchmark qw(:hireswallclock);
and run it again and we get
the code took:0.00105906 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU)
Ok still no result. Will I been playing with perl long enough to know there is a MTOWTDI to get out of this and that is to use Time::HiRes direly like this

So I just used


use Time::HiRes;
my $bm_s = Time::HiRes::gettimeofday();

at the start then finished off with

my $bm_e = Time::HiRes::gettimeofday();
printf("I ran in %.8f s\n", $bm_e - $bm_s);

and give it run and I get
I ran in 0.00112486 s

Yeah! So here are the results for 10 runs

  1. Pure Moose 0.010644

  2. Scaffold 0.1136693

  3. BuildInstance 0.138047


for all three once I got them all using the '_loadLSDClassesFromDir' in BUILD so Pure Moose is the fastest followed by Scaffold and BuildInstance. Just for kicks I ran the same tests with Scaffold before I made the BUILD changes and got '0.00131384' much faster of course as there is no overhead on reading files.

So it is easy to see the Pure Moose faster but only on a very micro scale. One does see that there is a price to pay for the file load as I ran the pure moose without the file load and it came out at only '0.005322' But I think most of us can take that '0.005' of a second. But then again that 1498.9623km in light speed which is a fair distance.

light moose.jpg

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations