python线程为什么这么慢,Python:为什么线程函数比非线程慢

Hello I'm trying to calculate the first 10000 prime numbers.

I'm doing this first non threaded and then splitting the calculation in 1 to 5000 and 5001 to 10000. I expected that the use of threads makes it significant faster but the output is like this:

--------Results--------

Non threaded Duration: 0.012244000000000005 seconds

Threaded Duration: 0.012839000000000017 seconds

There is in fact no big difference except that the threaded function is even a bit slower.

What is wrong?

This is my code:

import math

from threading import Thread

def nonThreaded():

primeNtoM(1,10000)

def threaded():

t1 = Thread(target=primeNtoM, args=(1,5000))

t2 = Thread(target=primeNtoM, args=(5001,10000))

t1.start()

t2.start()

t1.join()

t2.join()

def is_prime(n):

if n % 2 == 0 and n > 2:

return False

for i in range(3, int(math.sqrt(n)) + 1, 2):

if n % i == 0:

return False

return True

def primeNtoM(n,m):

L = list()

if (n > m):

print("n should be smaller than m")

return

for i in range(n,m):

if(is_prime(i)):

L.append(i)

if __name__ == '__main__':

import time

print("--------Nonthreaded calculation--------")

nTstart_time = time.clock()

nonThreaded()

nonThreadedTime = time.clock() - nTstart_time

print("--------Threaded calculation--------")

Tstart_time = time.clock()

threaded()

threadedTime = time.clock() - Tstart_time

print("--------Results--------")

print ("Non threaded Duration: ",nonThreadedTime, "seconds")

print ("Threaded Duration: ",threadedTime, "seconds")

解决方案

In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native threads from executing Python bytecodes at once. This lock is necessary mainly because CPython's memory management is not thread-safe. (However, since the GIL exists, other features have grown to depend on the guarantees that it enforces.)

This means: since this is CPU-intensive, and python is not threadsafe, it does not allow you to run multiple bytecodes at once in the same process. So, your threads alternate each other, and the switching overhead is what you get as extra time.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值