Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Optimizing the bejeezus out of a sub

by ant9000 (Monk)
on Jun 24, 2003 at 07:16 UTC ( [id://268410]=note: print w/replies, xml ) Need Help??


in reply to Optimizing the bejeezus out of a sub

It's already barebone as it is, as far as I can tell. The only idea that comes to my mind is to delay print calls by building up a message string, and print it just once at the sub end. Depending on the amount of memory available (and output expected) it might get you a tiny little bit of speed increase, if your $fh is opened on disk.

Replies are listed 'Best First'.
Re: Re: Optimizing the bejeezus out of a sub
by sgifford (Prior) on Jun 24, 2003 at 07:45 UTC
    Thanks, that did speed it up a little bit!

    Before:

    %Time ExclSec CumulS #Calls sec/call Csec/c  Name
     49.6   21.58 21.525 130000   0.0002 0.0002  FTS::printto
    
    After:
    %Time ExclSec CumulS #Calls sec/call Csec/c  Name
     48.8   20.83 20.710 130000   0.0002 0.0002  FTS::printto
    

      Does anybody know why this seems to make a difference? The print calls are of fairly short strings, and the filehandle I'm printing to is buffered (strace shows that each iteration is generating only one system call). I thought that printing a small string to a buffered filehandle would just append the string to the stdio buffer, which would be as fast as or faster than me buffering myself.

        As you say, you are generating a system call (plus memory copy from user to kernel space) for every iteration: that might be a small penalty, but incurs 130,000 times... doing it just once saves some bit work, hence the (small) performance increase.
        OTOH, if your strings get extremely big you are probably going to lose this benefit, since buffering in your sub is likely to cause a lot of work for memory allocation.
        Depending on the type of $fh, you may well be making a method call for each print. Perl's method calls are fairly expensive, and the time for most of the call counts against the caller, not the callee.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-19 08:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found