in reply to Efficient Looping Constructs
Here's an example..
Results:#!/usr/bin/perl -w use strict; use Benchmark; timethese(100000, { 'loop' =>'loop()', 'noloop' => 'noloop()' }); sub loop { my $x; for (1..10) { $x++; } } sub noloop { my $x; $x++; $x++; $x++; $x++; $x++; $x++; $x++; $x++; $x++; $x++; }
Now in this example, the for loop works just fine, but as you can see, not using a loop is three times as fast. %99.9 of the time, you're not going to have to optimize this unless you're calling this 100k times.Benchmark: timing 100000 iterations of loop, noloop... loop: 4 wallclock secs ( 3.48 usr + 0.03 sys = 3.51 CPU) @ 28 +490.03/s ( n=100000) noloop: 0 wallclock secs ( 1.09 usr + 0.00 sys = 1.09 CPU) @ 91 +743.12/s ( n=100000)
Anyway, I just thought I'd throw it out there..
Rich
|
|---|