| Paul Lalli 2006-01-10, 4:02 am |
| Sai Tong wrote:
> I have an array of many objects and I want to call a method on
> each of these objects and the save the returned values into an array:
>
> my @return_values;
> foreach my $retrievedObject (@array_of_objects) {
> push (@return_values , $retrievedObject->method );
> }
>
> The problem is this code runs too slowly for a large array of objects.
> Its seems that it might be a little too slow to "push" each
> returned value into @return_values . Can anyone suggest the best way to
> improve the speed of putting the returned value of
> each object into an array?
I find it drastically unlikely that "push" is the cause of your slow
down. On what basis do you make this assumption?
Try profiling your code, using the Devel::DProf module (standard with
Perl). That will tell you where your code is spending the most time.
I'm willing to bet it's in the method call, not the push operation.
Paul Lalli
|