Così ho messo a punto un codice di test per vedere come il modulo di multiprocessing si ridimensionasse sul lavoro con CPU rispetto al threading. Su Linux Ho l'aumento delle prestazioni che mi aspetto:python multiprocessing vs threading per lavoro con cpu legato su windows e linux
linux (dual quad core xeon): serialrun took 1192.319 ms parallelrun took 346.727 ms threadedrun took 2108.172 ms
mio dual core MacBook Pro mostra lo stesso comportamento:
osx (dual core macbook pro) serialrun took 2026.995 ms parallelrun took 1288.723 ms threadedrun took 5314.822 ms
Allora sono andato e l'ho provato su una macchina Windows e avuto qualche risultati molto diversi.
windows (i7 920): serialrun took 1043.000 ms parallelrun took 3237.000 ms threadedrun took 2343.000 ms
Perché oh perché, l'approccio di multiprocessing è molto più lento su Windows?
Ecco il codice di prova:
#!/usr/bin/env python import multiprocessing import threading import time def print_timing(func): def wrapper(*arg): t1 = time.time() res = func(*arg) t2 = time.time() print '%s took %0.3f ms' % (func.func_name, (t2-t1)*1000.0) return res return wrapper def counter(): for i in xrange(1000000): pass @print_timing def serialrun(x): for i in xrange(x): counter() @print_timing def parallelrun(x): proclist = [] for i in xrange(x): p = multiprocessing.Process(target=counter) proclist.append(p) p.start() for i in proclist: i.join() @print_timing def threadedrun(x): threadlist = [] for i in xrange(x): t = threading.Thread(target=counter) threadlist.append(t) t.start() for i in threadlist: i.join() def main(): serialrun(50) parallelrun(50) threadedrun(50) if __name__ == '__main__': main()
ho eseguito il codice di prova su un quad core Dell PowerEdge 840 in esecuzione Win2K3, ed i risultati non sono stati così drammatica come la tua, ma il vostro punto resta valida: serialrun preso 1266.000 ms parallelrun preso 1906.000 ms threadedrun preso 4.359,000 ms Sarò interessato a vedere quali risposte ottieni. Non mi conosco – Jeff