Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have a program which has a GUI and performs some very heavy mathematical computations for a couple of minutes and then outputs a result. When I try to directly interface it through its DLL's, it runs fine, but it doesn't run at 100% CPU usage like the GUI does. Why is this?

I understand the reason why computational heavy program might not run at 100% is due to I/O that it might be doing but this program doesn't do any, not even a single printf(). Files are being generated at the end of execution in both the GUI and DLL versions of the program. The interface program makes mulitple calls to the DLL during execution as well.

It is written in C, compiled using VS2008 CL compiler running in Release mode. Running on Intel i5 CPU with 4GB RAM (60% utilization average, never above 70%). When I set affinity to the process to one CPU (in this case, 1/4 "CPU's", 2 cores with 2 threads each), the GUI uses 100% of that dedicated CPU for about 2 min. My interfacing application uses anywhere from 60-80% CPU for about 5 minutes. I use Window's Performance Tab upon performing CTRL+SHFT+ESC.

---EDIT---

I found that database calls (there are a few specific ones) are being preempted a lot for extended periods of time (usually 5 times slower). Using Process Explorer I was able to find out that each program uses exactly the same amount of CPU time -> leading to my conclusion about preemption. For anyone that doesn't know about this tool, download Process Explorer from SysInternals, very useful.

---EDIT---

I found that the GUI opens a single static connection to the database. While my interface doesn't do that, leading to opening and closing the connection thousands of times. Opening it once for the lifetime brought the CPU up to 100%.

share|improve this question
1  
Please tell us how many cores your CPU has, what tool you use for measurement of CPU utilization, and exactly what you mean by "not at 100%". Like, about how much? –  Mike Nakis Jan 5 '12 at 16:09
1  
I/O is more than printf(). Do you have enough memory? –  ftr Jan 5 '12 at 16:10
    
Is it because the machine has multiple processor cores? A single threaded application is only going to run on 1 processor. –  maple_shaft Jan 5 '12 at 16:19
1  
Also: when you "directly interface it" are you constantly exchanging calls, or do you just tell it to start, and you wait until it stops? Also: are you sure it is not saving anything to any file? Also: please take a look at your memory utilization, as @ftr suggested. –  Mike Nakis Jan 5 '12 at 16:20
1  
Are you sure the GUI isn't taking up the additional CPU itself? –  Chris Pitman Jan 5 '12 at 17:34
show 10 more comments

4 Answers 4

up vote 13 down vote accepted

Profile it!

It's the only way to know exactly what is happening and what is using what resources and where it's being limited at.

share|improve this answer
    
Any programs you recommend for Windows 7 64bit? –  jsn Jan 5 '12 at 16:43
    
Quick&dirty profilng: debug a release build and break the process at random times. You'll gain an insight in what part of the code takes most of the time (statistically). –  quant_dev Jan 5 '12 at 16:53
    
You might also want to look at Process Monitor, which will show you what system calls are being used by a given process, if those might be the cause of waits and slowdowns. –  greyfade Jan 11 '12 at 0:16
    
@greyfade Thanks for suggestion, downloading. I figured out my program is very slow reading for the database (Access file). Hopefully this will help me out. –  jsn Jan 11 '12 at 15:46
add comment

Remember that your program is not the only one run by the OS. Each program has a priority assigned to it. You need to check that out and see if you can give you program a higher priority or reduce other running programs priorities when possible.

share|improve this answer
add comment

Many things may be causing this. One possibility is that there is some desynchronization logic involved in calling the DLL and receiving results from it, and this logic somewhere relinquishes a timeslice. (Sleep(0) will not relinquish the remainder of your timeslice; Sleep(1) will.)

If by any chance you are using an awful lot of memory, there may be some swapping going on; that would slow things down A LOT. Try disabling your swapfile, and see if you get an out of memory error. I use WinXP with 4GB of RAM and no swapfile, and my machine is FAST! (Though I run the danger of running out of memory, but I keep track of that.)

And, of course, the possibility that it is writing to a file as it works is worth checking. Could it be that as it is thinking, it is writing its thoughts down in a log file?

Also, instead of using the Windows Task Manager for looking at CPU utilization, do yourself a favor and get SysInternals' Process Explorer. It is much, much better, and it will give you a lot more information about your processes, your DLLs, the handles that are open, etc.

share|improve this answer
    
Disabling your swap file doesn't prevent paging, it just forces the system to only page read-only pages (like program code) rather than having the option to page program code or least recently used data. See my comment here for an example of why. –  Mark Booth Jan 5 '12 at 18:36
    
@MarkBooth thank you for the comment. I was aware of these objections but I went ahead and I tried disabling my swapfile just to try it, and I did notice a performance increase. Also, I am pretty sure Windows XP is not swapping anything on my machine, for the very simple reason that there exists no swap file. In any case, I am open to reconsider, but I will have to first see substantially better evidence than just the words of some "MVP". –  Mike Nakis Jan 5 '12 at 18:46
    
Disabling the page file does not disable paging. Also, many programs allocate lots of memory which they never use, if you have no page file then this has to stay in memory for the entire time the program is running. If you have a page file, this allocated memory (which is never referenced) can be paged out of physical memory leaving more physical memory for active data cache. Having more space for cache can easily result in a system with a page file running faster than one without one. Read this excellent answer and its refs for more info. –  Mark Booth Jan 6 '12 at 11:35
    
Regarding the use of Sleep() in Windows program, I'd follow this advice. –  user29079 Jan 9 '12 at 13:39
add comment

Depending upon what you are doing, maxing out the CPU is not the best way go and thus, the numbers you are getting back now might actually mean that things are running faster than they were before even though the processor is not maxed out. Even extremely complicated algorithms may not max out the CPU utilization and this can be a good thing as it means you can do other tasks while running the program.

Remember that when a program is running not all of the data may be in the processor cache and the time it takes to go out to the RAM (short from the processors perspective) or to other storage (e.g. hard drive, NAS, etc) (long from the processors perspective) is time that the processor can be running other tasks. Computers are very good at task switching and generally we use that to our advantage as well.

In terms of your program, doing anything with a GUI tends to be very slow as the GUI has to wait for the screen to repaint when updates are done and this can take an extremely long time to run from the processors standpoint. Most computationally heavy operations are written in such a way that they are decoupled from a GUI to ensure that they focus solely on their task and other tricks are used to report their status if they take awhile to run.

In terms of making your code as fast as possible, as Malfist noted in their answer profiling the code is your best bet and seeing what is running slow. Likewise, if possible making the algorithm run some parts in parallel will ensure it runs subjectively faster as it can take advantage of multiple cores on the processor. Additionally, you might want to write some very basic logging that notes when certain parts of the code is entered and exited (showing running time) and how many times certain operations are performed. These are typically given to you in a good profiler, but it can be a useful exercise to do on your own as well.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.