I am doing a java program which has two versions : one with a GUI and one without.
I am happy with the GUI one because it uses 100% of the CPU all the time but the one without (which is supposed to be faster) only uses 5-25% of the CPU. Both versions are exactly the same in terms of computations. The same problem happens when I run from Eclipse and from the shell.
I thought about allocating more memory with -Xms and -Xmx but it won't change the CPU usage.
I also tried to set the priority to nice my process to -15 in terminal but there was no noticeable difference.
How can I ensure, in Eclipse and in terminal that my code has 100% of the CPU.
-
Make your application multithreaded.Santosh– Santosh2013年08月27日 09:05:42 +00:00Commented Aug 27, 2013 at 9:05
2 Answers 2
I see 2 possibilities
Are you sure it is CPU bound? Drop an infinite loop in there and see if it spikes.
Is your PC quad core? Possibly 100% is 25 % for your PC and the GUI, being multithreaded uses the CPU better, perhaps.
Since you say this is non GUI version, do you go overboard with
println
, i.e non buffered printing?
Parallelism if possible will fix #2, and perhaps with some design changes even #1
5 Comments
100% CPU utilization is a problem, not a goal.
Usually, although I am sure there are exceptions, one would see a program beeing stuck at 100% CPU usage as an indication of an error.
The reason for this is that whenever your process uses peripherial components the program will wait for the component to return, and while the process is waiting it will pass control to other processes.. In this context peripherals include things like RAM.
So a single treaded program staying at 100% CPU usage means that it's cycling through instructions but never touching a very limited set of resources. In particular your program would not be touching RAM, in java this means that your program makes no function calls. - So probably your program is stuck.
Multi threaded programs is much more complicated since the program can pass control to other parts of the same program.
Ultimately the question you should be considering is; does the GUI program finish tasks faster than the non-GUI program? If it does then there are reasons to look at why that is, and most likely those reasons boil down to parallel processing*. If the GUI uses more time than the non-GUI then the low CPU usage is an added perk, when comparing the GUI program to the non-GUI program.
*Edited to add: programming for concurrent processing is a huge complication. Make sure the potential benefit is worth it before going down this path.