in reply to How expensive is eval { }?

If you don't know, just try:
use strict; use warnings; use Benchmark qw(cmpthese); no warnings 'void'; sub do_something { for (1..10){ $_*$_; } } cmpthese -1, { eval => sub { eval { for (1..10){$_*$_} } }, sub => sub { do_something() }, }; __END__ Rate sub eval sub 446836/s -- -0% eval 446836/s 0% --

This is not a very good benchmark, but if eval BLOCK was very slow you'd see it here.

Replies are listed 'Best First'.
Re^2: How expensive is eval { }?
by mscharrer (Hermit) on Apr 17, 2008 at 09:18 UTC
    Thanks,
    that's a good idea.

    I'm getting results from -3% to -6% in favor for sub on my machine. I might write a bigger benchmark and post the results here.

      I rerun the test several times and with different perl version (5.8.8 and 5.10.0), and got results from 0% to -4%.

      You can see that our both results have higher noise than the difference, so I wouldn't base a design decision on it. If exceptions are cleaner from a programming POV I'd go with exceptions.