Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have an event that does some processing and one of the lines in the event takes a long time to complete thus the stuff that happens in the event, very undesireably.

Is it common practice to separate out the thing that takes long (in this case it is a a line of code that calls a method that takes a long time) in the form of a thread?

For example, maybe I can have a boolean that can then be detected by the thread that continuously checks for this boolean. Or is there a more elegant way of approaching the problem?

What I have tried:

I have one event that runs a block of code, but there is a bottleneck at a time-consuming line of code.
Posted
Updated 27-Dec-21 17:54pm
Comments
PIEBALDconsult 28-Dec-21 16:20pm    
Yes.

1 solution

That largely depends on what else is going on in you program. For example, lets assume that the time-consuming code (TCC) takes 90% of the time needed for the procedure to complete. Handing that section off to a separate thread isn't going to make a great deal of difference to the overall runtime for the procedure. If, on the other hand, the time consuming code takes about 50% of the time needed, you should see a speed-up factor of about 2. That assumes that there's no inter-dependence between the critical section and the rest of the procedure. If the procedure needs the results of the TCC to proceed, then shipping that off to a separate thread won't improve things either.

Adding threads may make things worse. If the thread and the "main" procedure need to coordinate data access to shared variables, then the overhead of creating, acquiring and releasing mutexes might cause a severe negative impact on the overall runtime.

On the other hand, if the TCC is a good candidate for parallelization, then that might be the improve things greatly. E.G. if the calculation is some sort of loop, for which each pass uses a set of data that's independent from any other loop, then you might see great gains.

The best place to start though, is to examine the code in question. Is there another algorithm that will give the same results but at a lower cost? For example, if you need to sort your data, how efficient is your sort? If you're using a good sort, are you inadvertently triggering worst-case behavior? If you're using a collection which needs to be traversed, can a different collection provide better performance? Or is there a different traversal technique that will use less time? Will sorting the collection improve things? Maybe you're doing something that's artificially slowing things down when you don't need to, like forcing a data flush to disk on every I/O operation, when you might be able to get away with one data flush at the end of the calculation? There are many things that could be causing things to be slow. Analyze your code, and see if there's improvements that can be made before resorting to threads.
 
Share this answer
 
Comments
BillWoodruff 28-Dec-21 1:25am    
+5 a thorough, satisfying, reply !
k5054 28-Dec-21 9:41am    
Thank you for your critique. Made my day.
Admin BTA 28-Dec-21 6:25am    
There are some very good points. I have actually found "do something different" to be very useful when coding. I am measuring time for execution for various lines, code blocks, critical points in the code. I feel like a lot of that performance testing is automatable and was also wondering if there was something for C# that can run the app and then sort of let the dev know where, statistically, time is being spent most.

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