使用FilePointer读取二进制文件时CPU使用率高 [英] High CPU Usage while reading Binary File using FilePointer

查看:76
本文介绍了使用FilePointer读取二进制文件时CPU使用率高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何通过使用文件指针读取二进制文件时减少CPU使用率?
当我阅读二进制文件时,我的CPU使用率至少为53%.

如何减少呢?

有什么建议吗?

问候,
AJ83

Hi,

How to reduce CPU usage while reading binary file by using File Pointer?
Presently when I am reading Binary File, my CPU usage is 53% atleast.

How to reduce it?

Any suggestions??

Regards,
AJ83

推荐答案

CPU使用率高没有错.毕竟,我希望您的应用程序确实有一些工作要做,而不是像某些开发人员"那样,无所事事.如果操作系统和应用程序功能没有问题,是否担心在任务管理器中查看CPU使用率,这会使您感到难受吗?好吧,在这种情况下,请不要看它. :-)相反,请继续阅读这篇文章.

首先要了解的是:您正在使用抢先式多任务处理(多线程)系统;通常,它可以很好地知道"何时抢占线程并将其切换出去.即使此机制可能不是完美的,至少在中等工作量下并且在资源极为稀缺的情况下也是如此,但它在保持所有应用程序响应能力方面做得相当不错. 53%的CPU使用率完全不是问题的征兆.实际上,您确实希望CPU使用率很高.想一想.

请参阅:
http://en.wikipedia.org/wiki/Preemptive_multitasking [ http://en.wikipedia.org/wiki/Profiling_%28computer_programming%29 [ ^ ].

[END EDIT]

与您类似的担忧是很有效的,因为它浪费了CPU,并且某些应用程序使用CPU时间,而实际上却什么也没做.对于非常差的GUI应用程序来说,此缺陷是一个典型缺陷.如果UI应用程序不忙于任何任务;如果用户不执行任何输入,则GUI应用程序应严格将CPU时间花费为零.如果不是这种情况,那就是一个严重的愚蠢错误.用户输入应由硬件中断驱动.正确的GUI设计可确保GUI线程在花费CPU零的事件周期之间处于休眠状态.一些白痴有时使用用户输入的轮询"(拉模型),这是对资源的浪费,通常只是愚蠢的. 我确定这不是您的情况.

请参阅:
http://en.wikipedia.org/wiki/Event-driven_programming [ http://en.wikipedia.org/wiki/Event-driven_architecture [ http://en.wikipedia.org/wiki/Push_technology [ http://en.wikipedia.org/wiki/Inversion_of_control [
There is nothing wrong with high CPU usage. After all, I hope your application really has some work to do, not just fooling around with CPU cycles, as some "developers" do. If you have no problem with OS and application functionality, is your concern that you look at CPU usage in Task Manager, which makes your feeling bad? Well, in this case, just don''t look at it. :-) Instead, keep reading this post.

First thing to understand is: you are working with preemptive multitasking (multithreading) system; it generally "knows" pretty well when to preempt your thread and switch it out. Even though this mechanism may be not perfect, at least at the moderate workload and not in case of extremely scarce resources, it does a reasonably good work of keeping all applications responsive. The CPU usage of 53% is not a sign of trouble at all. Actually, you really want to have CPU usage pretty high; just think about it.

Please see:
http://en.wikipedia.org/wiki/Preemptive_multitasking[^].

Now, you also need to understand that the CPU usage of your code, which is, importantly, pretty much consecutive chain of CPU commands, has nothing to do with efficiency of your code and performance of your application. It''s important to complete your operation sooner, not spending less CPU resources. If you can optimize your code to have less CPU instructions to perform the same task, this would be wonderful and would really help, but it would not reduce CPU usage. The CPU usage will remain high, but you really want it to be high. What is important, is avoid wasting resources, not using than. You could, for example cooperatively yield CPU to other threads using Yield or Sleep methods, but would it improve anything in your particular case? No! It would only delay completion of your operations and actually increase the total CPU usage, due to the overhead of yielding (additional CPU time for activation and operation of the system thread scheduler and thread switching commands). Don''t try to do it!



One note of using Windows Task Manager: don''t reply on it: its measurements are generally very inaccurate and don''t really show what happens with processes. Usually, these measurements are not practical to be used for any judgement on application performance. For performance improvement, only a profiler can help:
http://en.wikipedia.org/wiki/Profiling_%28computer_programming%29[^].

[END EDIT]

The concerns similar to yours are quite valid is the CPU is wasted, and some applications use CPU time where they actually do nothing. This flaw is a typical one for really poor GUI applications. If a UI application is not busy with any task; and it the user does not perform any input, the GUI application should spend strictly zero CPU time. If this is not the case, there is a bad stupid mistake. The user input should be driven by hardware interrupts. Correct GUI design makes sure the GUI thread is sleeping between event cycles spending zero of CPU. Some morons sometime use "polling" (pull model) of the user input, which is a poor waste of resource and is just generally stupid. This is not your case, I''m sure.

Please see:
http://en.wikipedia.org/wiki/Event-driven_programming[^],
http://en.wikipedia.org/wiki/Event-driven_architecture[^],
http://en.wikipedia.org/wiki/Push_technology[^],
http://en.wikipedia.org/wiki/Inversion_of_control[^].

These references are just for your understanding; not for fixing something.

Don''t worry, be happy.

—SA


这篇关于使用FilePointer读取二进制文件时CPU使用率高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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