3

I have a file encryption program. When the program is encrypting files, it doesn't exceed 25% CPU usage, hence it is slow.

How can I make the OS assign to it more CPU load? (Such as WinRAR, when it compresses files, it reaches 100% from CPU load).


[Edit]: As my cores are 4, it doesn't use more than one core. How can I make it use the rest of cores?

Community
  • 1
  • 1
  • 2
    If it's not using all the CPU then it is I/O bound, i.e. it's spending time opening/saving the files EDIT: or is not using all CPU cores. You could increase performance by doing multiple encryptions in parallel and loading files while encrypting. – George Duckett Jul 06 '11 at 14:32
  • 2
    25% is a fishy CPU number; do you have a quad-core machine? – dlev Jul 06 '11 at 14:33
  • I have only one file encryption per time, could the FileStream's IO slow the process? I have intel i5 CPU –  Jul 06 '11 at 14:34
  • As I am using C#'s streams to IO, it is very related to C#. –  Jul 06 '11 at 14:42
  • @Mr.DDD: Did you try using PFX? Here is an [example](http://stackoverflow.com/questions/5347801/parallel-extensions/5348210#5348210). See my answer. – KMån Jul 06 '11 at 18:54

5 Answers5

2

Unless you are otherwise throttling the application it will use as much CPU as the OS allows it to - which should be up to 100% by default. I would guess that some other resource is the bottleneck.

Are you streaming the data to encrypt from a remote location? From a disk that is for some reason quite slow?

Yuck
  • 49,664
  • 13
  • 105
  • 135
  • Do you mean I should load the file first to RAM to get speed? But there is a problem, some files are too large for the RAM (Gigabytes) –  Jul 06 '11 at 14:49
  • 1
    Buffering is good for improving the performance of accessing remote resources. It's probably not (on its own) going to net you much of a performance benefit related to CPU usage. After reading some of the other answers here I'm leaning toward the notion that a single-threaded app is not benefiting from your multi-core CPU. You might try running the buffering operation on a new thread and doing your encryption in another... – Yuck Jul 06 '11 at 14:55
  • I want to learn more on threading. Is there any good resources? –  Jul 06 '11 at 15:00
1

If your tool is single threaded program, then it only consumes one core! And the performance will reach 100% on that core in case your program only do a for loop or other kind of loop. If the tool must do I/O then it never has maximum performance. And 25% you see is per all cpu cores. As I remember there some posts show you how to display the percentage of consumption on each cpu core!

longbkit
  • 1,218
  • 1
  • 13
  • 20
1

Sometimes people think a high CPU percent means an efficient program. If that were so, an infinite loop would be the most efficient of all.

When you have a program that basically processes files off a mechanical hard drive, ideally it should be IO bound, because reading the file simply has to be done. i.e. The CPU part should be efficient enough that it takes a low percent of time compared to moving the file off disk. Anything you can do to reduce CPU time will reduce the CPU percent, because I/O takes a larger percentage of the total, and vice-versa. If you can go back-and-forth between the two, reducing first CPU (program tuning), then I/O (ex. solid-state drive), you can make it really fly.

Then, if the CPU part is still taking longer than you would like, by all means, farm it out over multiple cores.

Community
  • 1
  • 1
Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135
1

Just in case, if you are using v4.0, rather than assigning more CPU load, try using Parallel Framework(PFX). It is optimized for multi core processors..

Parallel.Invoke(() => DoCompress());

Also, Threading in C# is the best threading related resource in the universe.

KMån
  • 9,896
  • 2
  • 31
  • 41
-1

This has nothing to do with the processor and assigning resources: the tool you use is simply not designed to use all (I guess) 4 cores of the cpu.

MADMap
  • 3,132
  • 3
  • 25
  • 31