Python Multiprocessing / Threads Takes More Time Than Single Processing on a Virtual Machine

I work on a virtual machine that sits on my company's mainframe.

I have 4 cores for which I am trying to parallelly process my Python code. I am not familiar with this yet, and I come across really unexpected behavior, namely that multiprocessing / threading takes longer than one processing. I can’t say that I am doing something wrong or the problem arises from my virtual machine.

Here is an example:

import multiprocessing as mg
import threading
import math
import random
import time

NUM = 4

def benchmark():
  for i in range(1000000):
    math.exp(random.random())

threads = []
random.seed()

print "Linear Processing:"
time0 = time.time()
for i in range(NUM):
  benchmark()
print time.time()-time0

print "Threading:"
for P in range(NUM):
  threads.append(threading.Thread(target=benchmark))
time0 = time.time()
for t in threads:
  t.start()
for t in threads:
  t.join()
print time.time()-time0

threads = []
print "Multiprocessing:"
for i in range(NUM):
  threads.append(mg.Process(target=benchmark))
time0 = time.time()
for t in threads:
  t.start()
for t in threads:
  t.join()
print time.time()-time0

The result of this is as follows:

Linear Processing:
1.125
Threading:
4.56699991226
Multiprocessing:
3.79200005531

, , . , , :

for t in threads:
  t.start()
  t.join()

:

Linear Processing:
1.11500000954
Threading:
1.15300011635
Multiprocessing:
9.58800005913

, , .

30% , , .

, , , .

+4
1

, -, , Macbook Pro, cPython 2.7.12, :

$ python test.py
Linear Processing:
0.733351945877
Threading:
1.20692706108
Multiprocessing:
0.256340026855

, :

for i in range(1000000):

To:

for i in range(100000000):

:

Linear Processing:
77.5861060619
Threading:
153.572453976
Multiprocessing:
33.5992660522

? - . , threading, - -. multiprocessing - .

, , Linear Processing , . , , 4 , . .

+5

Source: https://habr.com/ru/post/1650285/


All Articles