4 ms·
For 4-cores CPU running 64-bit Linux with Python 2.6: * single-threaded: 7.6s * two threads: 8.8s * 4 threads: 9.14s * 4 processes (via multi
by d0mine 17y ago
For 4-cores CPU running 64-bit Linux with Python 2.6:
* single-threaded: 7.6s
* two threads: 8.8s
* 4 threads: 9.14s
* 4 processes (via multiprocessing) 2.0s
Overhead is ~16-20% (5% for multiprocessing)
#!/usr/bin/env python
from threading import Thread
from multiprocessing import Process
def countdown(n):
while n > 0:
n -= 1
COUNT = 100000000
def run_once():
countdown(COUNT)
def run_with_threads(nthreads, make_thread=Process):
threads = [make_thread(target=countdown, args=(COUNT//nthreads,))
for _ in range(nthreads)]
for t in threads:
t.start()
for t in threads:
t.join()