Skip to content Skip to sidebar Skip to footer

Python Asynchronously Increment The Same Variable To Boost Performance?

How can I use multiple threads to increment same variable concurrently in parallel so that the total amount of time taken is reduced to that multipleth of the original synchronous

Solution 1:

Multi-threading perfomance boost isn't as linear as you might think.

Assuming you have a 4-core CPU (nc=4), going from single-thread (t=1) to 4 threads (t=4) should show you an decrease in overall computation time. You might expect a 75% decrease in computation time if you have a 4-core CPU, but in the real world, some work still needs to be done to keep the OS going.

Having more threads than you have physical cores would (t>nc), at best, give you the same performance as matching those values (t=nc), but in the real world, managing resources with t>>nc would probably actually run a bit slower.

Next, your approach is to use a single counter, updated by all threads, this adds complexity as you'd need a mutex/semaphore/etc. to ensure the counter updates function as expected with multiple parties reading/writing to it - the more parties, the harder this gets, and, you guessed it, yet another performance hit.

I'd recommend dividing your problem space into the number of threads (t=nc, if you were reading above) such that (e.g.) threadA processes 0-24, threadB: 25-49, threadC 50-74 and threadD: 75-99 - this way they don't need to talk to each other, as each is working on their own slice of the problem space.

tl;dr:

  • when multi-threading, there's rarely any advantage to go beyond t=nc, but functionally, you may even want to stop at t=(nc-1) to leave some CPU time for your OS/etc.
  • thread synchronization needs to be done carefully; try to slice the pie into t chunks before sending the jobs to the threads.

Post a Comment for "Python Asynchronously Increment The Same Variable To Boost Performance?"