I've been playing with Term::ReadKey for a few days and read somewhere that poorly written code using the ReadKey function could bog down a CPU. I decided to write a small program and see what would happen:
#!/usr/local/bin/perl use Term::ReadKey; use Time::HiRes; use strict; my @char; ReadMode 4; # Turn off controls keys while (lc $char[0] ne "q") { while (not ($char[0] = ReadKey(-1))) { # Time::HiRes::sleep 0.1; } print $char[0]; select((select(STDOUT), $| = 1)[0]); #flush STDOUT buffer } ReadMode 0; # Reset tty mode before exiting
I ran the above code under Solaris 7/5.6.1, Solaris 8/Perl 5.6.1 and Solaris 7/Perl 5.005_02. Note that these are disparate CPUs. It's the trends I concerned with. I then eyeballed top and came up with some interesting results:
1) When the statement Time::HiRes::sleep 0.1; was commented out, the programs CPU utilization rose steadily to levels of 25, 50 and 85% (respectively) over a 4 minute period of time and then stayed at that level.

2) When the statement Time::HiRes::sleep 0.1; was not commented out, the program CPU utilization started out low (0.06, 0.09, 1.83%) and decreased to 0% after two minutes.

I guess that something in the OS is capping the poor utilization in case 1, but even so, the keyboard response times do not appear to suffer. OTOH, why does the utilization fall to zero for case 2, again regardless of keyboard activity?

Apparently, a little sleep can prevent a lot of pain. It would be interesting to see if similar results are achieved on different platforms. Would anyone like to share some insight on this behaviour? Also, is there a better way besides sleep to achieve more desirable CPU performance?

If the code and the comments disagree, then both are probably wrong. -- Norm Schryer


In reply to Sleep, don't Weep by jlongino

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.