Advertisement
surik00

random benchmark

Nov 14th, 2019
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. import random
  2. from timeit import timeit
  3.  
  4.  
  5. def test_randint():
  6.     return random.randint(1, 2)
  7.  
  8.  
  9. def test_pure_random():
  10.     return random.random()
  11.  
  12.  
  13. def test_pure_random_binary():
  14.     return 1 if random.random() < 0.5 else 2
  15.  
  16.  
  17. if __name__ == '__main__':
  18.     print(timeit(test_randint), 'test_randint')
  19.     print(timeit(test_pure_random), 'test_pure_random')
  20.     print(timeit(test_pure_random_binary), 'test_pure_random_binary')
  21.  
  22. """
  23. 1.306905230000666 test_randint
  24. 0.13657626399981382 test_pure_random
  25. 0.1529860969994843 test_pure_random_binary
  26. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement