Guest User

Untitled

a guest
Jan 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. # TODO Figure out how threads work
  2. # TODO Do a Fibonacci counter
  3.  
  4. import concurrent.futures
  5.  
  6.  
  7. def fib(pos, _tpe):
  8. """Return the Fibonacci number at position."""
  9. if pos < 2:
  10. return pos
  11.  
  12. x = fib(pos - 1, None)
  13. y = fib(pos - 2, None)
  14.  
  15. return x + y
  16.  
  17.  
  18. def fibp(pos, tpe):
  19. """Return the Fibonacci number at position."""
  20. if pos < 2:
  21. return pos
  22.  
  23. x = tpe.submit(fib, (pos - 1), tpe).result()
  24. y = tpe.submit(fib, (pos - 2), tpe).result()
  25.  
  26. return x + y
  27.  
  28.  
  29. if __name__ == '__main__':
  30. import sys
  31.  
  32. with concurrent.futures.ThreadPoolExecutor() as cftpe:
  33. fun = fibp if len(sys.argv) is 3 else fib
  34. position = int(sys.argv[1])
  35. print(fun(position, cftpe))
  36. print(fun.__name__)
  37.  
  38. $ time python test_fib_parallel.py 35
  39. 9227465
  40. fib
  41.  
  42. real 0m3.778s
  43. user 0m3.746s
  44. sys 0m0.017s
  45.  
  46. $ time python test_fib_parallel.py 35 dummy-var
  47. 9227465
  48. fibp
  49.  
  50. real 0m3.776s
  51. user 0m3.749s
  52. sys 0m0.018s
Add Comment
Please, Sign In to add comment