最佳使用循环或计时器是什么? [英] what is the best using loop or timer?

查看:116
本文介绍了最佳使用循环或计时器是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

计时器最适合处理器和物理内存吗?...?

解决方案

嗨!我假设您正在寻求延迟.

如果是这样,我唯一知道延迟循环是个好主意,那就是在固件开发过程中,当您是操作系统的编写者并且您已经知道在您执行此操作期间不需要其他任何CPU时延迟.如果您是在这种环境下进行开发的,则编译器随附的库通常会提供某种非常紧凑的延迟函数(想到的延迟函数大约只有15个CPU指令长),并且提供了非常精确的延迟-以上情况,精度为25纳秒.

在所有其他应用程序中,尤其是在多任务,多进程或多线程操作系统中(适用于大多数平台,包括移动平台,当然也包括所有主要操作系统[Unix,Linux,VMS,Windows]),通常,操作系统会提供一种提供延迟的方法.计时器就是这样一种解决方案.在Windows中,普通方法是Kernel32.DLL(或Kernel32.lib,如果您需要将对函数的调用链接到程序中)中称为"Sleep()"函数的函数.如果您使用的是.NET库,则视情况而定,它至少提供了十二种输入等待时间的不同方法.上面的Sleep()函数的.NET库简单替代品是System.Threading.Thread.Sleep().

如果您的环境不提供睡眠功能,我建议您寻找它提供的任何等待"服务,并使用适合您的应用程序的一种.使用此方法可使操作系统有机会使进程处于保持状态",从而在进程等待时间间隔过去时消耗零CPU时间.这有助于将CPU负载保持在尽可能低的水平.对于大多数应用程序来说,这是一个很好且重要的策略.

使用循环延迟的商业应用程序(不好的主意)由于无法使用(因此不值得您的潜在客户付钱),原因是它们施加在系统上的CPU负载量很大.

希望对您有所帮助.
--Vic


循环可能会冻结UI,因此使用计时器会更好,但总的来说,取决于您要执行的操作.


are the timer is best for processor and the physical memory....?

解决方案

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


A loop could freeze the UI so a timer would be better, but in general, depends on what you are trying to do.


这篇关于最佳使用循环或计时器是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆