Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import math
- #print(math.sin(3))
- #from math import sin, cos, pi
- #print(sin(3), pi)
- #import math as m
- #print(m.sin(3))
- #from math import sin as f
- #from math import *
- -------------------------------------
- t = timeit.timeit('pi*2', 'from math import pi')
- print(t)
- ---------------------------------------
- pi = 3.14
- t = timeit.timeit('pi*2', 'from __main__ import pi')
- print(t)
- ---------------------------------------
- import random
- n_stog = 1000
- random.sample(range(10), 3)
- stog = [random.random() for _ in range(n_stog)]
- igly = ([random.random() for _ in range(500)] + random.sample(stog, 500))
- --------------------------------------
- >>> len(set(igly)) == len(set(stog))
- True
- >>> len(set(igly)) & len(set(stog))
- 1000
- -----------------------------------------
- import timeit
- MAX_EXPONENT = 5
- # define tests
- TEST_IN = '''
- fount = 0
- for x in needles:
- if x in haystack:
- found += 1
- '''
- TEST_SETS = '''
- len(needles & haystack)
- '''
- TEST_LISTS_F_LISTS = '''
- len(set(needles)) & len(set(haystack))
- '''
- NUMBER = 100
- # define setup
- SETUP_LISTS = '''
- import random
- random.seed(81)
- haystack = [random.random() for _ in range(1000)]
- needles = ([random.random() for _ in range(500)] + random.sample(haystack, 500))
- '''
- SETUP_SETS = '''
- import random
- random.seed(81)
- haystack = {random.random() for _ in range(1000)}
- needles = ({random.random() for _ in range(500)} | set(random.sample(haystack, 500)))
- '''
- tests = [('in_lists', TEST_IN, SETUP_LISTS),
- ('in_sets', TEST_IN, SETUP_SETS),
- ('sets', TEST_SETS, SETUP_SETS),
- ('sets from lists', TEST_LISTS_F_LISTS, SETUP_LISTS),]
- t = timeit.timeit(TEST_IN, setup=SETUP_LISTS, number=NUMBER)
- print(t)
Add Comment
Please, Sign In to add comment