Click here to Skip to main content
15,887,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a large program which does a lot of counting and calculation behind the scenes. When it enters this calculation stage, the windows form only has this to do, but I've set it as a background task "background worker" functions etc.

The calculating takes a large amount of time and I've done reading about how to minimise this. Possible (and followed through tips) were:

- decreasing module sizes
- passing data by reference/pointers
- not using gradient colors
- rendering allocated memory space free after use
- not using fancy graphics

Now, the problem is the main calculation is the only thing actually occuring once selected, and the user has to sit looking at a screen saying "calculating". Not even a moving "..."

To put it into perspective:
I have the same program in a seperate c++ file which is not windows forms - standard c++ that just does the calculation. It finished in about 5-10 mins depending on what the user inputs. In windows forms, it's been 2 hours and still running. I've checked the files it is writing to and I've found it's averaging at a shockingly low rate of calculation. Each result takes about 20 seconds, or more, whereas in the std c++ it's too fast to count per second! :D

Any ideas what I could check for, or any feedback on what you've found has helped with your program speed would be much appreciated!
Posted
Comments
[no name] 2-Apr-13 20:26pm    
So why not use the existing C++ code in a DLL that is P/Invoked from your winform app if it that much faster?
Sergey Alexandrovich Kryukov 2-Apr-13 20:50pm    
It's not very usual that a form application is too slow. The reason is: why? Before we figure it out, the question makes no sense.
—SA
lostandconfused1234 3-Apr-13 6:58am    
I have no idea what to look for SA: that's the problem :)
Sergey Alexandrovich Kryukov 5-Apr-13 0:43am    
Hard to help in such situation; you have to look at nearly everything, but most usual problem is general design mistakes or lack of sound design, rather then particular algorithms...
—SA

1 solution

Without being able to look at your code I can only guess at what might be causing the slowdown which is what you need to know in order to make an effective change.
There are however 3 outline possibilities I can think of.

1 Your calculation simply takes that long.
2 Your non calculation e.g UI code is taking up the processor time.
3 Your application is spending a lot of time thrashing between threads.

You have excluded option 1 by isolating your calculation code. Now you need to run your application without performing the calculation and use Windows Task Manager or SysInternals Process Explorer to determine what the processor usage of the rest of the application is.
If neither part of the application is consuming excessive proessor time when run without the other then you may need to look at any data that is shared between threads and how access to this is being synchronised. Is your process actually wasting most of the time waiting for access to data locked by a thread that is not currently executing?
 
Share this answer
 
v2
Comments
lostandconfused1234 3-Apr-13 8:03am    
thank you for your ideas. I have done as you suggested; the cpu usage is 0 when the operation isnt active - after it switches display - as expected as it is literally doing nothing but displaying buttons. (obviously when theyre clicked it increases slightly before back to zero) but when the background operation begins the cpu usage rapidly increases to roughly around 40-45% on the .exe.
It is clearly this calculation which is causing the long time. I will readdress my code and look for obvious problems but as far as I can tell there isn't a huge amount of data switching. The calculating is generally just inside modules with minimal transfer only when needed, and like I said, it works fine standalone. Maybe it has something to do with the progress report. Thanks again, if you have any more possibilities to mind let me know, but I realise without me showing code it's just guess work.
Matthew Faithfull 3-Apr-13 11:31am    
Interesting. A process taking far longer than needed but self limiting at about 50% processor usage can be an indication of a locking issue or just that the calculation runs on only 1 of 2 cores. If it's easy enough to do then I'd start a second parallel calculation thread. If the CPU goes up to 100% but both calculations are done in ~120% of the time it took to do 1 then you're out of luck your calculation is just locked to a single core and limited by its speed, perhaps try SSE2 compiler options.
On the other hand if the CPU is still stuck at 50%-75% with 2 calculation threads running then you definitely have a locking/thrashing issue, something is interrupting your calculation thread probably by using all of its time slice waiting for some locked resource. If 50% of the time is spent waiting you can only get 50% of the potential processor performance and everything takes twice as long.
lostandconfused1234 3-Apr-13 13:52pm    
I'm concerned this is a bit out of my league. I'm using a background worker. Does that not use two cores? Else, if it's not overly hard to do could you send me in the direction of some tutorial? I assume msdn has something somewhere? Many thanks !
Matthew Faithfull 3-Apr-13 16:44pm    
What I'm suggesting would ammount to simply creating a second background worker identical to the one you already have which as long as you have no complex shared data should be easy enough, i.e. just run the code that creates a background worker twice instead of once. Then check how this changes the perormance and the processor load.
Windows will use the cores available within reason so whether a second core is used is only down to whether you have one. What sort of hardware are you running on?
lostandconfused1234 6-Apr-13 10:36am    
It's fixed now :-) thanks a lot for your help!

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