Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Journal of LTjake (4001)

Thursday May 21, 2009
12:33 PM

Benchmark

[ #39015 ]

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!

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.