Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import multiprocessing
- import threading
- import time
- import random
- def hello(thid, n):
- i = 0
- res = 0
- while i < n:
- res += i
- i += 1
- print("%d. result: %d"%(thid, res))
- def test_process(n):
- tlist = []
- for i in range(4):
- t = multiprocessing.Process(target=hello, args=(i,n,))
- t.start()
- tlist.append(t)
- for t in tlist:
- t.join()
- def test_thread(n):
- tlist = []
- for i in range(4):
- t = threading.Thread(target=hello, args=(i,n,))
- t.start()
- tlist.append(t)
- for t in tlist:
- t.join()
- def test_sima(n):
- for i in range(4):
- hello(i,n)
- if __name__ == '__main__':
- n = 3000*1000
- s = time.time()
- test_thread(n)
- print("Multi-Thread: %.3fs" % (time.time() - s))
- s = time.time()
- test_process(n)
- print("Multi-Process: %.3fs" % (time.time() - s))
- s = time.time()
- test_sima(n)
- print("Single: %.3fs" % (time.time() - s))
- print("Done!")
Advertisement
Add Comment
Please, Sign In to add comment