Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Re: Re: Optimizing the bejeezus out of a sub

by sgifford (Prior)
on Jun 24, 2003 at 18:47 UTC ( #268639=note: print w/replies, xml ) Need Help??

in reply to Re: Re: Optimizing the bejeezus out of a sub
in thread Optimizing the bejeezus out of a sub

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.

  • Comment on Re: Re: Re: Optimizing the bejeezus out of a sub

Replies are listed 'Best First'.
Re: Re: Re: Re: Optimizing the bejeezus out of a sub
by ant9000 (Monk) on Jun 25, 2003 at 08:33 UTC
    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.
      The print is buffered, so only generates a system call when the buffer is full. My template is small and the buffer size is larger than it, so I only have one system call for the entire template.
      #!/usr/bin/perl for(my $i=0;$i<4000;$i++) { print $i%10; }
      $ strace -c -e write /tmp/t20 >/tmp/out
      % time     seconds  usecs/call     calls    errors syscall
      ------ ----------- ----------- --------- --------- ----------------
      100.00    0.000034          34         1           write
      ------ ----------- ----------- --------- --------- ----------------
      100.00    0.000034                     1           total
Re: Re: Re: Re: Optimizing the bejeezus out of a sub
by Elian (Parson) on Jun 24, 2003 at 19:17 UTC
    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?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2022-01-22 18:55 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (63 votes). Check out past polls.