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:#!/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
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.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?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.
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sleep, don't Weep
by jwest (Friar) on Aug 23, 2001 at 00:47 UTC | |
|
Re: Sleep, don't Weep
by Mungbeans (Pilgrim) on Aug 22, 2001 at 16:05 UTC |