0

I am implementing two algotihms that run in a multi-agent framework, so each agent should run the algorithm. The first algorithm is sequential, each agent should wait for the previous one, but the second one is concurrent.

I implemented both with threads and the running time is more or less the same for both compared with the serial case(for the sequential was expected but not for the concurrent one). I changed the code to use processes and the time is 20x slower for both algorithms. Is this normal?

Edit 1: the idea of the algorithm is that each agent run it concurrently, then return parameters from the algorithm to the main program to compute the average. This average is used for the next iteration of the algorithm and so on... The return of the parameters, which i am doing with a multiprocessing queue, could be the problem of the slow time?

enter image description here

xDazz
  • 67
  • 4
  • For threads, Python's Global Interpreter Lock means you probably won't get better performance with multiple threads than with one thread. With processes, you should be able to get better performance if the amount of work each sub-process does is large enough to offset the cost of creating and starting up the new process. Note that communication between processes will be more expensive than communication between threads, so multiprocessing will work best when the amount of communication between processes is minimized. – Jeremy Friesner Jun 15 '16 at 18:13

1 Answers1

1

Threads vs. Processes

Why threads could be faster:

  • Spawning a new process is much slower than starting a new thread.
  • Communicating between processes is much slower than communicating between threads.
  • Threads need less memory than processes.
  • If you're on a single-processor machine, threads should have similar performance to processes (given the above caveats).

Why processes could be faster:

  • If you're on a multi-processor machine, processes will be able to use all the processors simultaneously for computation and give you better performance.
  • If one process crashes, it won't bring down the other processes, while if one thread crashes (not an exception, an actual interpreter
    crash), it will bring down all the threads.
Collateral.dmg
  • 128
  • 1
  • 10