In reference to
why code can be so slow? I am creating this seperate node to get the most performance out of your (new) CGI script.
I have received many replies from the Perlmonks that were of value where to start optimizing that slow script. Still, there are a lot of unanswered questions where code should (not) be optimized, ... This node will be dealing with those questions and experiences I get of having a script which is merely giving 1 hit/sec to a normal amount of hits which should be atleast 50 times more. I will try to keep this node up-to-date as possible to help other monks with the same problem of having speed-i-gitis.
I have received the following hints to optimize a script to be getting better performance than before:
Development tools: (jbert)
Ways to improve speed:
- Do not use non-persistent CGI's (snowhare)
they are slow especially when you use a lot of modules they can be the timesucker of your system.
For me they are using CPU currently because of all the pre-loading.
- Use mod_perl (scorpio17)
This is probably one of the better upgrades to be done for perlscripts to be run. Modules get loaded once in memory and stay there till you use them; no more pre-loading, swapping and extensive memory/cpu usage because of that.
- Optimize your algorithms (halley)
Optimizing your algorithms and functions is the fastest way to go to a fastest script. Which I used to do 2 years ago is already superseeded by faster ways because of the never ending learning curve; - Search for alternative and faster ways
Lots of these methods can already be used in a matter of seconds by browsing at cpan to find your module to get your favorite thing done. Most of the authors on these modules get feedback from their users and put development time to get their modules not only bug free but also faster, snappier and up-to-date to the newest technologies..
- Use Apache::Registry (moron)
if you got non mod_perl mode to be using these nice features of mod_perl - Use Apache2 (talexb)
Although, I'm pretty sceptical about this, I've been testing with Apache2 myself on a single core CPU, multi user systems, Apache2 is significantly slower even when optimized. I still wonder if this would increase my overal web and script performance or rather decrease? It made me go back to Apache v1.3; I got to note this is more than a year and a half ago. Anyone has experience on this ?
Unanswered questions:
- Are there any references available which to look for when writing optimized (web) perl applications?
- I've been thinking "use" and "no" would be of use inside a subroutine to save memory at that time but found out the hard way the "no" did nothing at all to the memory. Probably using "no" will be even a performance hit everywhere over my scripts?
- Is there any speed difference in using if $blah { &dothis } or &dothis if $blah?
- Keeping security in mind, lots of parsing has to be done to protect input from being malicious. My way of parsing input has always been "passive"; I will not create an error message with "wrong input" (except for hard-core input errors) but rather filter out the bad input and get the rest to the script; are there any good modules filtering out bad input?
- Does it help to have a dual core or quad cpu core for perlscripts?
- Which is fastest? RDBMS (sql) with DBI, DBD or DBM? BerkeleyDB3 or 2? ..
- Which is best whenever you got to access many files through your script; (ex: gathering metadata from XML files) all automatically? Use a database to cache the files on disk next time they got used? are there methods for this?
- Sometimes you need to get one value, for example a MD5 Hash; with non-persistant cgi's (as I understand) once you load the module it cannot be unload again and it will stay in memory; are there any ways to get such value anyways without using the memory overhead?
- When loading a module, does it load the -entire- module or do I only load the code which needed?
- Anyone else has questions to add? comments? links? experiences with above (or even more) problems which to watch out for?
Probably a lot more of questions/hints and tips will be added to this list; I hope to help the web developers around here with a problem which apparantly strikes us all; which probably also can be avoided by having the know-how of which to use, what to (not) do and especially what to watch out for.
Many thanks already to the monks for the contribution to the previous SOPW.