wa12rior

3.12.2019 Python

Dec 3rd, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 KB | None | 0 0
  1. #import math
  2. #print(math.sin(3))
  3.  
  4. #from math import sin, cos, pi
  5. #print(sin(3), pi)
  6.  
  7. #import math as m
  8. #print(m.sin(3))
  9.  
  10. #from math import sin as f
  11.  
  12. #from math import *
  13. -------------------------------------
  14.  
  15. t = timeit.timeit('pi*2', 'from math import pi')
  16. print(t)
  17.  
  18. ---------------------------------------
  19.  
  20. pi = 3.14
  21.  
  22. t = timeit.timeit('pi*2', 'from __main__ import pi')
  23. print(t)
  24.  
  25. ---------------------------------------
  26.  
  27. import random
  28.  
  29. n_stog = 1000
  30.  
  31. random.sample(range(10), 3)
  32.  
  33. stog = [random.random() for _ in range(n_stog)]
  34.  
  35. igly = ([random.random() for _ in range(500)] + random.sample(stog, 500))
  36.  
  37. --------------------------------------
  38.  
  39. >>> len(set(igly)) == len(set(stog))
  40. True
  41. >>> len(set(igly)) & len(set(stog))
  42. 1000
  43. -----------------------------------------
  44.  
  45. import timeit
  46.  
  47. MAX_EXPONENT = 5
  48.  
  49. # define tests
  50. TEST_IN = '''
  51. fount = 0
  52. for x in needles:
  53.    if x in haystack:
  54.        found += 1
  55. '''
  56.  
  57. TEST_SETS = '''
  58. len(needles & haystack)
  59. '''
  60.  
  61. TEST_LISTS_F_LISTS = '''
  62. len(set(needles)) & len(set(haystack))
  63. '''
  64.  
  65. NUMBER = 100
  66.  
  67. # define setup
  68.  
  69. SETUP_LISTS = '''
  70. import random
  71. random.seed(81)
  72.  
  73. haystack = [random.random() for _ in range(1000)]
  74.  
  75. needles = ([random.random() for _ in range(500)] + random.sample(haystack, 500))
  76. '''
  77.  
  78. SETUP_SETS = '''
  79. import random
  80. random.seed(81)
  81.  
  82. haystack = {random.random() for _ in range(1000)}
  83.  
  84. needles = ({random.random() for _ in range(500)} | set(random.sample(haystack, 500)))
  85. '''
  86.  
  87. tests = [('in_lists', TEST_IN, SETUP_LISTS),
  88.          ('in_sets', TEST_IN, SETUP_SETS),
  89.          ('sets', TEST_SETS, SETUP_SETS),
  90.          ('sets from lists', TEST_LISTS_F_LISTS, SETUP_LISTS),]
  91.  
  92.  
  93. t = timeit.timeit(TEST_IN, setup=SETUP_LISTS, number=NUMBER)
  94. print(t)
Add Comment
Please, Sign In to add comment