Head-to-Head: perl's eval, Exception.pm, Exception/Class.pm and Error.pm
I wanted to use a more OO approach to error handling in perl instead of just plain old eval
, so I did some looking around. I wanted it to be easy, so I looked at these modules:
I also found this article on perl exception handling: Object Oriented Exception Handling in Perl. That sounded great and all, but what would I be giving up?
I don’t know that much about perl internals, but I thought that code installed as a $SIG{__DIE__}
signal handler would be faster than eval
handling. I was wrong. Check this out:
I have these three small test scripts (indentation screwed up with the post):
eval.pl:
exception_class.pl:
error.pl:
exception.pl:
How did these compare in execution speed?
-
exception.pl:
23 wallclock secs (22.09 usr + 0.11 sys = 22.20 CPU) @ 6756.76/s (n=150000)
-
error.pl:
9 wallclock secs ( 7.90 usr + 0.02 sys = 7.92 CPU) @ 18939.39/s (n=150000)
-
exception_class.pl:
2 wallclock secs ( 1.37 usr + 0.00 sys = 1.37 CPU) @ 109489.05/s (n=150000)
-
eval.pl
0 wallclock secs ( 0.41 usr + 0.00 sys = 0.41 CPU) @ 365853.66/s (n=150000)
Holy smokes! That’s a huge difference in speed. I’ll be sticking with eval for now, thanks.