in reply to How expensive is eval { }?
A simple benchmark indicates that the overhead isn't very much (unless the benchmark is completely flawed of course). Consider:
use strict; use warnings; use Benchmark qw(cmpthese); cmpthese ( -1, { BareFor => \&dofor, ExceptFor => \&doefor, ForExcept => \&dofore, } ); sub dofor { my $sum = 0; for (1 .. 1000) { $sum += $_; $sum += $_; $sum += $_; } } sub dofore { my $sum = 0; for (1 .. 1000) { eval {$sum += $_;} } } sub doefor { my $sum = 0; eval { for (1 .. 1000) { $sum += $_; $sum += $_; $sum += $_; } } }
Prints:
Rate BareFor ExceptFor ForExcept BareFor 1626/s -- -2% -11% ExceptFor 1661/s 2% -- -9% ForExcept 1834/s 13% 10% --
which indicates that the eval overhead is close to the cost of 2 += operations - not a whole lot in other words!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How expensive is eval { }?
by moritz (Cardinal) on Apr 17, 2008 at 09:33 UTC | |
by GrandFather (Saint) on Apr 17, 2008 at 10:05 UTC |