Last active
December 5, 2016 23:12
-
-
Save bonfy/1c6ba24eb226206275255efbc6ce0845 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import multiprocessing | |
def timerecord(func): | |
def wrapper(*args, **kwargs): | |
start = time.time() | |
func(*args, **kwargs) | |
end = time.time() | |
print 'COST: {}'.format(end - start) | |
return wrapper | |
def fib(n): | |
if n<= 2: | |
return 1 | |
return fib(n-1) + fib(n-2) | |
@timerecord | |
def nomultiprocess(): | |
fib(20) | |
fib(20) | |
@timerecord | |
def hasmultiprocess(): | |
jobs = [] | |
for i in range(2): | |
p = multiprocessing.Process(target=fib, args=(20,)) | |
p.start() | |
jobs.append(p) | |
for p in jobs: | |
p.join() | |
if __name__ == '__main__': | |
nomultiprocess() | |
hasmultiprocess() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment