OK, so your system is capable of running something like 150 perl CGI requests/sec, for a CGI which does very little (the printenv.cgi).

So the difference is in what your code does, so you need to work out what it is doing which is slow and then think about whether that is reasonable amount of time for that work and if you can speed it up if not. (If your code downloads 1Mbyte from a remote site and you are bandwidth limited then tweaking your code won't really help).

Measuring which bits of your code take time is called profiling. There are some good perl modules to help with this, e.g. Devel::Profile and Devel::DProf. These are easier to use on a command line application than a CGI, so one thing you might want to do is to first get to a stage where you can run your CGI as a command line app.

If your CGI doesn't rely on any parameters to demonstrate the slowness, you can try simply: perl my.cgi < /dev/null > output.html. If this reproduces the slowness, then you can try using the profiling tools to work out where it is spending it's time.

Profiling is a bit of an art, and it is very easy to read the results wrongly, so I'd always recommend using more than one approach or tool, to cross-check your data. In particular, simply using Time::HiRes and adding some logging with hires timing to your app can help show where time is being spent.

If your application isn't CPU bound (and it's quite likely not to be), then profiling may be of less help. In this case, you need to work out what your code is doing. The strace utility can be good for this, but again, can require some expertise to interpret. You can turn on timestamps from the strace output which can help you work out where the time is going.

And as a last thought (and perhaps the thing to try first), if your code is connecting to other servers you could be getting hurt by connection times, DNS lookup times etc. You could try time ping some.host to get an idea of the time taken for a process on your box to resolve 'some.host' and get a network packet to and from the remote host. If ping doesn't work for you, you can always use time wget some://url.on.that.host if you are fetching a remote HTTP page, say.


In reply to Re: Why can code be so slow? by jbert
in thread Why can code be so slow? by freakingwildchild

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.