I reproduced the time difference on Mac OS X (real 1.120s vs. 0.381s).

The time difference seems to disappear when I change the server code to read:

... my $pid = fork; if ( ! defined $pid ) { die "Can't fork: $!" } if ( $pid ) { close $sock; } else { while( 1 ){ ...

(Each "..." is code I didn't change. The salient changes are (1) it will die when it can't fork, and (2) the parent will close the socket it won't be using anyway.)

I'm not sure why having the parent leave the socket open causes the problem you're having, but it doesn't surprise me that it causes some problem.

Also, you should be checking (in the client and in the server) that your sockets are created successfully. In some runs, but not all, I got a message that implies to me that the call to IO::Socket::INET->new() failed.


In reply to Re: I know two prints are slower than one, but 200x slower?!?!? by kyle
in thread I know two prints are slower than one, but 200x slower?!?!? by rsmah

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.