The problem is in the @ready = $sel->can_read(0.0001); The thing is that this returns a socket only whenever there is a new socket or whenever a socket is closed. Otherwise it waits those 0.0001 seconds. Which may seem small but for 10*1024 (that's how many characters my test script tried to read) this means 1.024s wasted. And is it really necessary to check for new sockets 10000 times a second?;-)

This script

use strict; use warnings; use IO::Socket; use Time::HiRes qw(gettimeofday tv_interval); my $sock = IO::Socket::INET->new(PeerAddr => 'localhost', PeerPort => '19', Proto => 'tcp'); my $start_time = [gettimeofday]; my $buff; for (1..10) { read $sock, $buff, 1024; } print "Taken " . tv_interval($start_time) . "seconds\n"; $sock->close();
took approximately 10s to read those 10*1024 characters with your code, no matter if I ran just one or eight of them. With this change:
use constant NEW_SOCK_EVERY => 1000; ... my $server_sock = new IO::Socket::INET( Listen => 1,LocalPort => 19,Reuse=> 1 ); my $sel = new IO::Select( $server_sock ); my $i = NEW_SOCK_EVERY; while($server_sock) { if (++$i>=NEW_SOCK_EVERY) { $i=0; foreach my $socket ($sel->can_read(0.0001)) { if($socket == $server_sock ) { new_socket($socket); }else{ if(defined ($socket)) { close_socket($socket); } } } } foreach my $wsocket ($sel->can_write(0.0001)) { gen_chars($wsocket); } } ...
the time went down to about 2s. I think it's enough to test for new clients 10 times a second, don't you? ;-)

Another option that seems to work (though I can't find it mentioned in the IO::Select's docs) is to specify a negative timeout for the can_read(). That seems to bring the time down to 1.1-1.3 seconds. And it's a much smaller change to your code :-)


In reply to Re: chargen program is too slow / IO::Select socket handle outputting timing issue by Jenda
in thread chargen program is too slow / IO::Select socket handle outputting timing issue by kabeldag

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.