1
time_1 = time.time()
num = 1000000
for i in range(num):
    print i
time_2 = time.time()
print time_2 - time_1

13.1949999332

def time_test_pool(num):
    print num

if __name__ == "__main__":
    time_1 = time.time()
    num = 1000000
    pool = ThreadPool(8)
    pool.map(time_test_pool, range(num))
    pool.close()
    pool.join()
    time_2 = time.time()
    print time_2 - time_1

15.8250000477

Did I misunderstand the usage of pool? Why the pool is so slow?

Leo Hsieh
  • 351
  • 4
  • 12

1 Answers1

1

Python threads are not actually running in parallel but time-sliced. The reason is because the python interpreter is not thread-safe.

Said that, python threads are handy when you need to do lots of IO-bound stuff, but it will simply add overheads when trying to perform CPU-bound tasks (like yours).

The solution is typically using python.multiprocess (see python multi-threading slower than serial?), however in your case I am not sure it is going to improve things since the amount of work you do per-thread is very little and the only thing you are paying is the context switch.

Try to assign more iterations per thread, or consider using C++ which supports real multi-threading.

Community
  • 1
  • 1
simpel01
  • 1,792
  • 12
  • 13