Click here to Skip to main content
16,003,319 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Comments
Sergey Alexandrovich Kryukov 18-Oct-12 12:26pm    
Could you tell me why reducing CPU usage?
--SA

1 solution

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!

[EDIT]

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
 
Share this answer
 
v4
Comments
CPallini 18-Oct-12 14:08pm    
5.I suppose a profiler would help. After all I guess 'reading a binary files' is usually a not CPU intensive task (dominated by I/O). My 2 cents.
Sergey Alexandrovich Kryukov 18-Oct-12 14:42pm    
Thank you, Carlo. You see, the profiler is secondary, only used if there are real "performance leaks". It won't "improve" CPU usage, which is just not a valid criterion for quality of code, at least, not in such cases.
By the way, your note of non-CPU-intensive operation is a good point. It depends though. If input is cached (so actually all hardware part of reading could be just one bulk operation, or so) and calculations are relatively extensive, hardware operation costs could be negligible.
--SA
JackDingler 18-Oct-12 15:26pm    
Reading binary files is not necessarily a CPU intensive task. but in this case the I/O is buffered, so there is some background copying going on.
Sergey Alexandrovich Kryukov 18-Oct-12 15:45pm    
I agree. This is what I say.

You see, basically, this is irrelevant to OP's concern. CPU usage is only high because there is only one or two threads massively using CPU and not yielding (which is right thing), so, no matter what they do, OS gives them reasonable CPU time slices, that's it. It does not require anything special, as soon as CPU is not wasted for something stupid.
--SA
JackDingler 19-Oct-12 10:48am    
I have clients who call in almost every day expressing concerned that the CPU utilization remains at about 50% on their servers. I tell them it's because they are working at processing records, which leads to revenue... It's a good thing.

They want to know how to bring the load down... I'm tempted to say, close your doors, don't let clients in.

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