Click here to Skip to main content
15,888,221 members
Please Sign up or sign in to vote.
1.50/5 (4 votes)
See more:
are the timer is best for processor and the physical memory....?
Posted
Comments
Ed Nutting 18-Aug-12 23:34pm    
For which programming language? What would the loop/timer be doing? How frequently would the operation occur? Is it multi or single threaded? How many cores are you programming for? What defines "better" - responsiveness? Power consumption? CPU time used? Memory used?

Your question is far too vague. Please use the Improve Question link above.
Ed

Hi! I am assuming you are looking to create a delay.

If so, the only place I know of that a loop for a delay is a good idea is in firmware development when YOU are the writer of the Operating System and you already KNOW that the CPU is not going to be needed for anything else during your delay. If you are developing for such an environment, often the library that comes with your compiler will offer some type of delay function that is very compact (one that comes to mind is only about 15 CPU instructions long) and provides very precise delays -- in the above case, with 25-nanosecond accuracy.

In all other applications, especially in a multi-tasking or multi-process or multi-threading operating system (which applies to most platforms out there, including mobile platforms, certainly all major operating systems [Unix, Linux, VMS, Windows]), often the operating system will offer a method of providing a delay. A timer is one such solution. In Windows, the normal method is a function called 'Sleep()' function in Kernel32.DLL (or Kernel32.lib if you need to link the call to the function into your program). If you're using the .NET Library, it provides at least a dozen different ways to enter a wait period, depending on circumstances. The .NET Library simple replacement for the Sleep() function above is System.Threading.Thread.Sleep().

If your environment doesn't provide a sleep function, I recommend looking for whatever "wait" services it provides and use one of them that suits your application. Using this approach gives the operating system the opportunity to put your process "on hold", thereby consuming zero CPU time while your process is awaiting the time interval to elapse. This helps keep the CPU load as low as possible. This is a good and important policy for most applications.

Commercial applications which use loops for delays (bad idea) can be unusable (and thus not worth paying for by your potential clients) due to the amount of CPU load they put on the system.

I hope this helps.
--Vic
 
Share this answer
 
A loop could freeze the UI so a timer would be better, but in general, depends on what you are trying to do.
 
Share this answer
 
Comments
Espen Harlinn 19-Aug-12 9:27am    
5'ed!
Abhinav S 19-Aug-12 12:55pm    
Thank you Espen.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900