How to reduce CPU usage when the code causes continous 80-100% CPU running normally.

In this example, I was trying to create hash codes for 14K files. The first thing to do is to try and use an algorithm which does not use that much CPU. In my case, that meant using a crc32 algorithm instead of the MS md5 class.

But this just reduced the CPU for 100% to 80% - it still remained at 80% for half an hour or more.

In such cases, we have to work around this. Because basically what is happening is that one by one if we generate the hash codes for so many files, it will consume that much CPU.

The solution is to put Thread sleeps in between, and if you want like me, you can vary the sleep as per the size of the file, because the high CPU is caused not by the number of files alone, but by the size of the files.

Put an adequate amount of sleep in between the hash code generation code so that, the CPU is able to do other work in between. You can experiment by changing the sleep and observing how the CPU goes up and down.

I heard that using BeginInvoke instead of Task based threading can speed up i/o operations. However, in my tests, I found no such behavior. The only major change in utilization other than the sleep was when I changed the algorithm.

Comments

Popular posts from this blog

Tutorial: Using Google Cloud Storage from C# and .NET

Late 2008 Macbook only giving 1.5 gb/s speed with 6 gb/s Intel SSD?

Enable Visual Studio to use more than 2GB of memory