Hi, I have a slight problem that I'm hoping the community here might be able to help me with.
The project I am currently working on requires that I work with a number of matrices. To facilitate this, I have represented each matrix as an object with corresponding methods such as clone, multiply etc. The problem I am having is that in the algorithm I am working with I must create deep copies of the matrices an inordinate number of times. I have been doing this using my 'clone' method and using smallprof I have determined that the clone method is the bottleneck in my code, taking up around 80% of the runtime of the code.
I was hoping someone could look at the method I have written and offer any possible improvements. I am currently using the accessors module which I know can slow things down a little. Also, $self->Matrix is an array ref for a 2D matrix. Below is the code:
sub Clone { my $self = shift @_; my $matrix = $self->Matrix; return Matrix->new( "Matrix" => [ map ( { my $row = $_; [ map + ( { ( $_ ) ; } (@$row) ) ] } (@$matrix) ) ] ); }
Thank you in advance for any help anyone has to offer. While I realize it might be an unavoidable problem, I figured it was worth a shot to ask. According to smallprof, this method gets called 35Million times when running my test data, so an improvement / suggestions would be greatly appreciated. Also, I have tried a few other ways to do this but this has been the fastest so far.
In reply to Performance problem with Clone Method by Commandosupremo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |