import time
import multiprocessing
def totaltime(ct):
st = time.time()
a = 100.33
b = 23.33
for v in range(ct):
b = 1 + b
c = a * b
print("total time:", time.time()-st)
ct = 25000000
if __name__ == '__main__':
for i in range(multiprocessing.cpu_count()):
p = multiprocessing.Process(target=totaltime, args=(ct,))
p.start()
for p in multiprocessing.active_children():
print('Child process name: ' + p.name + ' id: ' + str(p.pid))
这段代码以前要 5s ,现在同一台机器只要 3s 了
import sys, time
stdout = sys.stdout
BAILOUT = 16
MAX_ITERATIONS = 1000
class Iterator:
def __init__(self):
print('Rendering...')
for y in range(-39, 39):
stdout.write('\n')
for x in range(-39, 39):
i = self.mandelbrot(x/40.0, y/40.0)
if i == 0:
stdout.write('*')
else:
stdout.write(' ')
def mandelbrot(self, x, y):
cr = y - 0.5
ci = x
zi = 0.0
zr = 0.0
i = 0
while True:
i += 1
temp = zr * zi
zr2 = zr * zr
zi2 = zi * zi
zr = zr2 - zi2 + cr
zi = temp + temp + ci
if zi2 + zr2 > BAILOUT:
return i
if i > MAX_ITERATIONS:
return 0
t = time.time()
Iterator()
print('\nPython Elapsed %.02f' % (time.time() - t))
这段以前要 1.2s ,现在同一台机器只要 0.95s 了
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.