Benchmark

LTjake on 2009-05-21T17:33:30

As noted in my last post, I was able to get a bit of a speed boost based on observations made as a result of code profiling.

In general, if I want to see if one piece of code is faster than another, I use Benchmark. Benchmark is shipped as part of the core set of modules, so there's no need to load up CPAN to get started. Its simplest usage, and the one i prefer looks something like this:


    use Benchmark ();
    
    Benchmark::cmpthese( $count, {
        Foo1 => sub {
            # code to do Foo1 here
        },
        Foo2 => sub {
            # code to do Foo1 here
        },
    } );

Of note is that $count can be negative, which will then signify how many seconds to run instead of the number of times. The result looks like this:


             Rate Foo1 Foo2
    Foo1 108665/s   -- -38%
    Foo2 175460/s  61%   --

It's pretty easy to see that Foo2 was faster. Using the above it was easy for me to test the XS-based ANSI parser vs the pure Perl version.

4k worth of ANSI over 10 seconds yields the following:


         Rate    PP    XS
    PP 15.7/s    --  -96%
    XS  379/s 2316%    --

For giggles, i tested it against a 33k ANSI, giving:


         Rate    PP    XS
    PP 2.23/s    --  -96%
    XS 58.7/s 2528%    --

Looks like a success to me!