Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Being more Assert-ive with Perl

by Zed_Lopez (Chaplain)
on Sep 18, 2004 at 00:28 UTC ( [id://391927]=note: print w/replies, xml ) Need Help??


in reply to Being more Assert-ive with Perl

You might like to look at Carp::Assert. Its assert function is liable to be slower than your test or die constructs, and it doesn't allow you to customize your die statements (unless you use its longer affirm routine, which is bound to be substantially slower.) I find it nice and readable, though. If I have time, I'll benchmark it later.

An idea you might like to steal from it is to append if DEBUG to your assertions, where DEBUG is a constant that you can set to 1 in development/testing and to 0 in production. That way, you have all of the benefits of assertions in development, and none of the overhead in production (along with none of the benefits.)

Replies are listed 'Best First'.
Re^2: Being more Assert-ive with Perl
by stvn (Monsignor) on Sep 18, 2004 at 15:58 UTC
    If I have time, I'll benchmark it later.

    I was already benchmarking for the other responses, so I did it for you.

    Here is the code

    #!/usr/bin/perl use strict; use warnings; use Benchmark qw(:all); use Carp::Assert; sub CarpAssert { my ($test) = @_; assert($test < 50) if DEBUG; return $test; } sub OrAssert { my ($test) = @_; ($test < 50) || die "Test is wrong"; return $test; } my @nums = map { ((rand() * 100) % 50) } (0 .. 100); cmpthese(10_000, { 'CarpAssert' => sub { eval { CarpAssert($_) } for (@nums) }, 'OrAssert' => sub { eval { OrAssert($_) } for (@nums) }, });
    Here is the results of this:
    Benchmark: timing 10000 iterations of CarpAssert, OrAssert... CarpAssert: 11 wallclock secs ( 9.92 usr + 0.04 sys = 9.96 CPU) @ 10 +04.02/s (n=10000) OrAssert: 8 wallclock secs ( 7.14 usr + 0.03 sys = 7.17 CPU) @ 13 +94.70/s (n=10000) Rate CarpAssert OrAssert CarpAssert 1004/s -- -28% OrAssert 1395/s 39% --
    I then tried turning off Carp::Assert, and it was only 3% faster than the OR assertions.
    Benchmark: timing 10000 iterations of CarpAssert, OrAssert... CarpAssert: 9 wallclock secs ( 6.50 usr + 0.03 sys = 6.53 CPU) @ 15 +31.39/s (n=10000) OrAssert: 10 wallclock secs ( 6.72 usr + 0.02 sys = 6.74 CPU) @ 14 +83.68/s (n=10000) Rate OrAssert CarpAssert OrAssert 1484/s -- -3% CarpAssert 1531/s 3% --
    I then changed the OR assertion to use a no-op error handler (as if they were off). Here is the modified sub
    sub noOpError {} sub OrAssert { my ($test) = @_; ($test < 50) || noOpError("Test is wrong"); return $test; }
    And surprisingly it slowed OR down
    Benchmark: timing 10000 iterations of CarpAssert, OrAssert... CarpAssert: 6 wallclock secs ( 6.54 usr + 0.00 sys = 6.54 CPU) @ 15 +29.05/s (n=10000) OrAssert: 7 wallclock secs ( 7.11 usr + 0.02 sys = 7.13 CPU) @ 14 +02.52/s (n=10000) Rate OrAssert CarpAssert OrAssert 1403/s -- -8% CarpAssert 1529/s 9% --

    An idea you might like to steal from it is to append if DEBUG to your assertions, where DEBUG is a constant that you can set to 1 in development/testing and to 0 in production. That way, you have all of the benefits of assertions in development, and none of the overhead in production (along with none of the benefits.)

    Personally I do not like to turn off my assertions, I also see my debug statements as different from assertions. Besides IMO, having the if DEBUG at the end of the statement would really ruin the readability of it all.

    -stvn

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://391927]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-23 07:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found