linux 线程解绑定,python多处理与线程对于Windows和Linux上的cpu绑定工作

所以我敲了一些测试代码,看看多处理模块如何在线程上调整cpu绑定工作。在Linux上,我获得了我期望的性能提升:

linux (dual quad core xeon):

serialrun took 1192.319 ms

parallelrun took 346.727 ms

threadedrun took 2108.172 ms我的双核macbook pro显示了相同的行为:

osx (dual core macbook pro)

serialrun took 2026.995 ms

parallelrun took 1288.723 ms

threadedrun took 5314.822 ms然后我去了Windows机器上试了一下,得到了一些非常不同的结果。

windows (i7 920):

serialrun took 1043.000 ms

parallelrun took 3237.000 ms

threadedrun took 2343.000 ms为什么哦,为什么,多处理方法在Windows上这么慢?

这是测试代码:

#!/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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值