Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

which is more efficient....

Status
Not open for further replies.

geebers

Technical User
May 29, 2009
20
US
Which taxes my cpu more.....

while :; do
echo "hello world"
done

OR

while :; do
echo "hello world"
sleep 1
done

just curious. sorry if its painfully obvious and i'm just a dolt.
 
The amount of work the CPU does is the same (until you stop the loop), except that it is spread over a much greater amount of time in second case. So if you stop it after a minute in both cases, then your CPU would have done an awful lot more in first case than second, because it would have been involved in an awful lot more loops that offset the overhead of calling "sleep"
 
Hmmm.... then i wonder if something like the following would be better at waiting for an event....

nc -l -u localhost 2222 | while read line; do
echo "Hey its $line"
./runMyScript.bash
done

Is there perhaps a better way to wait for text to come across port 2222 and and when it does, do this or that and then continue to wait for the next text string? Thanks for the help.
 
Hi

geebers said:
[gray](...)[/gray] and then continue to wait [gray](...)[/gray]
Again, I am not familiar with UDP, but with TCP [tt]netcat[/tt] handles only one connection then terminates.

There is an extremely old tcputils package containing a tool [tt]mini-inetd[/tt] which should better fit your expectation.

However if you want a long running script, eventually serving multiple connections in the same time, better use [tt]inetd[/tt] or [tt]xinetd[/tt]. Just another hint, no idea what are you trying to achieve.


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top