Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Why can code be so slow?

by jbert (Priest)
on May 01, 2007 at 09:17 UTC ( #612940=note: print w/replies, xml ) Need Help??

in reply to Why can code be so slow?

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 to get an idea of the time taken for a process on your box to resolve '' 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:// if you are fetching a remote HTTP page, say.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2020-07-07 00:42 GMT
Find Nodes?
    Voting Booth?

    No recent polls found