我编写了 3 个不同的代码来比较有线程和没有线程。基本上测量通过使用线程节省了多少时间,结果没有任何意义。
这是我的代码:
import time
def Function():
global x
x = 0
while x < 300000000:
x += 1
print x
e1 = time.clock()
E1 = time.time()
Function()
e2 = time.clock()
E2 = time.time()
print e2 - e1
print E2 - E1
Run Code Online (Sandbox Code Playgroud)
当我运行这个时,我得到以下输出:
26.6358742929
26.6440000534
Run Code Online (Sandbox Code Playgroud)
然后我编写了另一个函数,如下所示,将计数到 3 亿拆分为计数 3、1 亿:
import time
def Function():
global x
x = 0
while x < 100000000:
x += 1
print x
def Function2():
global x
x = 0
while x < 100000000: …
Run Code Online (Sandbox Code Playgroud) python time multithreading python-multithreading python-multiprocessing
我有这个非常简单的功能,我正在尝试运行和测试,但是,它没有输出任何东西,它也没有任何错误.我已多次检查代码,但没有任何错误.
我打印了工作,这是我得到的:
[<Process(Process-12, stopped[1])>,
<Process(Process-13, stopped[1])>,
<Process(Process-14, stopped[1])>,
<Process(Process-15, stopped[1])>,
<Process(Process-16, stopped[1])>]
Run Code Online (Sandbox Code Playgroud)
这是代码:
import multiprocessing
def worker(num):
print "worker ", num
return
jobs = []
for i in range(5):
p = multiprocessing.Process(target = worker, args = (i,))
jobs.append(p)
p.start()
Run Code Online (Sandbox Code Playgroud)
这是我期待的结果,但它没有输出任何东西:
Worker: 0
Worker: 1
Worker: 2
Worker: 3
Worker: 4
Run Code Online (Sandbox Code Playgroud)