Profiling and Optimizing Perl
with Devel::NYTProf and XS

Peter Karman

http://www.peknet.com/slides/optimizing/

Premature Optimization

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil." -Donald Knuth

Mature Optimization

  • What about mature optimization?
  • How do I know which 3% to worry about?
  • Is it the algorithm or the language?

The Three Percent

Devel::NYTProf

Devel::NYTProf examples

http://svn.peknet.com/perl/profiling

Observations about Perl as a language

Taking the plunge into C and XS

Stay away from XS

Render unto C what is C's, and render unto Perl what is Perl's.

#include your C

/* pure C helpers */
#include "search-tools.c"

/***************************************************/
MODULE = Search::Tools       PACKAGE = Search::Tools

PROTOTYPES: enable

Keep your XS minimal

boolean
is_ascii(string)
    SV* string;

    CODE:
        RETVAL = st_is_ascii(string);

    OUTPUT:
        RETVAL

Late nights and hair loss

If performance matters, it's worth it

XS vs PP Search::Tools Tokenizer
                          Rate pure-perl-greek xs-greek pure-perl-ascii xs-ascii xs-ascii-heatseeker-qr
pure-perl-greek         1171/s              --     -60%            -87%     -95%                   -96%
xs-greek                2959/s            153%       --            -66%     -87%                   -91%
pure-perl-ascii         8696/s            643%     194%              --     -62%                   -74%
xs-ascii               22727/s           1841%     668%            161%       --                   -32%
xs-ascii-heatseeker-qr 33333/s           2747%    1027%            283%      47%                     --

Further Reading

Credits