Running Python is unexpectedly slower

I decided to find out how multithreading is done in Python, and I did a comparison to find out what kind of performance enhancement I would get on a dual core processor. I found that my simple multi-threaded code is slower than the sequential equivalent, and I cannot understand why.

The test I developed was to generate a large list of random numbers and then print the maximum

from random import random
import threading

def ox():
    print max([random() for x in xrange(20000000)])

ox()It takes about 6 seconds to complete my Intel Core 2 Duo, and it ox();ox()takes about 12 seconds.

Then I tried calling ox () from two threads to find out how quickly this would complete.

def go():
    r = threading.Thread(target=ox)
    r.start()
    ox()

go()it takes about 18 seconds to complete, with two results being printed within 1 second of each other. Why should it be slower?

, ox() , Windows ox() python, 75% , . Python , max(), ?

+5
3
  • Python GIL. - Python . C- ( Python) .
  • Python GIL . , , , , .

Python . , multiprocessing, Python, .

. , Haskell (Data Parallel Haskell)

+9

random() . . . .

+1

, Python GIL . python, , , , Ray GIL.

Ray:

from random import random
import ray

ray.init()

@ray.remote
def ox():
    print(max([random() for x in range(20000000)]))

%time x = ox.remote(); y = ox.remote(); ray.get([x, y])

ox() 1,84 , ray 1,87 , .

Ray , . (https://ray.readthedocs.io/en/latest/using-ray-on- a-cluster.html https://ray.readthedocs.io/en/latest/autoscaling.html).

: Ray.

0

All Articles