On Thu, Oct 12, 2006 at 10:18:31PM +1300, Robin Sheat wrote: > I have a section of a program that is this: > > my $users = get_all_users(); > foreach my $user (@$users) { > my $details = get_user_details($user); > my $sum=0; > my $count=0; > for (my $i=1; $i<@$details; $i+=2) { > $sum += @$details[$i]; > $count++; > } > $means{$user} = $sum/$count; > } > > The $users var is a reference to an array that is about 450,000 entries. The > $details var is an array reference typically of size around 500 or so (note > however, the inner loop goes up in twos). Overall the innermost part is > executed 100 million times. How can I optimise it? If I was using C, I'd use > pointer arithmetic on the array to save the multiplication on the array > indexing. A naive reading of this implies that Perl would be doing a multiply > for every array access, is this the case, or is it optimised away? If not, is > there a way to use pointer arithmetic (or something that behaves similarly, > but probably safer)? It is probably possible to restructure the array so that > it can go up in ones, and do a foreach, but in most cases it'll be common to > want both values, which is why I didn't do that.
Not really a beginners topic, but ... In general, perl ops are expensive. Assuming you have enough memory and are CPU bound you'll want to be reducing the number of ops. This whole thing is a bit of a black art and can often yield surprising results so it is important to benchmark. I'd probably try replacing the middle of your loop with something like: my ($sum, $count, $i) = (0, @$details / 2, 1); $sum += @$_ for grep $i ^= 1, @$details; > Anyone know any good resources for this kind of thing? Nicholas Clark has a talk on this subject: http://www.ccl4.org/~nick/P/Fast_Enough/ You might even find newer versions of the talk floating around somewhere. -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>