This is my first post to the monastery, so please take it easy on me :) I use Perl mostly as a hobby, and I have been slowly learning by reading various books and lurking around here. So, I think I finally have a question worth asking (most of the time a Super Search reveals that my question has already been answered). I have started to use Moose for anything object oriented and have found it to be very nice.
I recently wrote an application that creates a few thousand Moose objects. It seemed to run a bit slow so, I used Devel::DProf to help locate the bottleneck in my code. I was convinced that one of my methods would be involved. To my surprise, it seemed to be the combination of using Moose::Util::TypeConstraints with Regexp::Common
I have tried to reduce this down to a simple case based on the example given in Moose::Cookbook::Basics::Recipe4.
If I create the subtype using the technique in the recipe:
it seems to run slower than if I create the subtype like this:subtype USZipCodeA => as Value => where { /^$RE{zip}{US}{-extended => 'allow'}$/ };
The code that I use for benchmarking and profiling the difference between these two techniques is behind the readmore tags.my $zip_re = qr/^$RE{zip}{US}{-extended => 'allow'}$/; subtype USZipCodeB => as Value => where { $_ =~ $zip_re; };
In AddressB.pm:package AddressA; use Moose; use Moose::Util::TypeConstraints; use Regexp::Common 'zip'; subtype USZipCodeA => as Value => where { /^$RE{zip}{US}{-extended => 'allow'}$/ }; has 'zip_code' => (is => 'rw', isa => 'USZipCodeA'); 1;
Comparing A and B:package AddressB; use Moose; use Moose::Util::TypeConstraints; use Regexp::Common 'zip'; my $zip_re = qr/^$RE{zip}{US}{-extended => 'allow'}$/; subtype USZipCodeB => as Value => where { $_ =~ $zip_re; }; has 'zip_code' => (is => 'rw', isa => 'USZipCodeB'); 1;
#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); use AddressA; use AddressB; my $zip = 12345; cmpthese (-5, { A => sub { AddressA->new(zip_code => $zip);}, B => sub { AddressB->new(zip_code => $zip);} } ); exit;
I'm using the following:
Rate A B A 2976/s -- -43% B 5187/s 74% --
I made a couple simple programs to create 10,000 objects using either technique. Then I used Devel::DProf to profile the code. The two highest values of 'ExclSec' for the creation of 10,000 AddressA objects were from these methods:
and for AddressB objects none of the Regexp::Common methods show up and these two methods have the highest values of 'ExclSec'.Regexp::Common::_decache Regexp::Common::new
I don't want to focus too much on the DProf results, since dprofpp is giving me negative elapsed time values and some negative 'CumulS' values. I have no idea why.Class::MOP::Instance::new Class::MOP::Instance::set_slot_value
I just tried this out on Mac OS X with perl 5.8.8 (macports) and the results of the comparison are (same Moose and Regexp::Common versions)
So, it's not as noticeable on Mac OS X. I'm not sure why this differs so much from strawberry perl.Rate A B A 2955/s -- -7% B 3174/s 7% --
My question has a few parts:
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |