Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # TODO Figure out how threads work
- # TODO Do a Fibonacci counter
- import concurrent.futures
- def fib(pos, _tpe):
- """Return the Fibonacci number at position."""
- if pos < 2:
- return pos
- x = fib(pos - 1, None)
- y = fib(pos - 2, None)
- return x + y
- def fibp(pos, tpe):
- """Return the Fibonacci number at position."""
- if pos < 2:
- return pos
- x = tpe.submit(fib, (pos - 1), tpe).result()
- y = tpe.submit(fib, (pos - 2), tpe).result()
- return x + y
- if __name__ == '__main__':
- import sys
- with concurrent.futures.ThreadPoolExecutor() as cftpe:
- fun = fibp if len(sys.argv) is 3 else fib
- position = int(sys.argv[1])
- print(fun(position, cftpe))
- print(fun.__name__)
- $ time python test_fib_parallel.py 35
- 9227465
- fib
- real 0m3.778s
- user 0m3.746s
- sys 0m0.017s
- $ time python test_fib_parallel.py 35 dummy-var
- 9227465
- fibp
- real 0m3.776s
- user 0m3.749s
- sys 0m0.018s
Add Comment
Please, Sign In to add comment