This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from timeit import timeit | |
import pandas as pd, numpy as np | |
import matplotlib.pyplot as plt | |
N = range(500, 100001, 500) | |
setup1 = 'import numpy as np; n = {}; fv = np.frompyfunc("{{}}_{{}}".format, 2, 1); a = np.arange(n); b = a + a.size' | |
stmt1 = "fv(a, b)" | |
from_func = [ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import List | |
def non_intersecting(L1: List[List[str]], L2: List[str]) -> List[List[str]]: | |
"""Return a new list that contains the original lists inside L1 that do not | |
contain any of the string in L2 | |
>>> non_intersecting([['e', 'h', 'c', 'w'], ['p', 'j'], ['w', 's', 'u']], ['k', 'w']) | |
[['p', 'j']] | |
""" | |
bad = set(L2) | |
result = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [1]: import numpy as np | |
In [2]: lst = list(range(100000)) | |
In [3]: import random | |
In [4]: idx = random.sample(lst, 100000//2) | |
################################################################################## | |
# List comprehension vs naive for-loop, list comprehension less than twice as fast |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import matplotlib.pyplot as plt | |
from timeit import timeit | |
gen = 'sum(1 for x in seq if x < 0)' | |
floop = ''' | |
result = 0 | |
for x in seq: | |
if x < 0: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import matplotlib.pyplot as plt | |
from timeit import timeit | |
sc = '[i for i in range({})]' | |
sg = 'list(i for i in range({}))' | |
N = range(1, 1000001, 10000) | |
tc = [timeit(sc.format(n), number=10) for n in N] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
stmt = "-1 in mydict.values()" | |
setup = "mydict = dict(zip(range({0}),range({0})))".format | |
N = range(0, 100001, 1000) | |
def timer(n, k): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
import timeit | |
import pandas as pd | |
import itertools | |
def flatten(nested): | |
result = [] | |
for sub in nested: | |
for x in sub: | |
result.append(x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
from collections import namedtuple | |
def namedtuple_me(series, name='S'): | |
klass = _get_class(tuple(series.index), name) | |
return klass._make(series) | |
@functools.lru_cache(maxsize=None) | |
def _get_class(fieldnames, name): | |
return namedtuple(name, fieldnames) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [21]: import numpy as np | |
In [22]: arr = np.arange(1, 10000) | |
In [23]: lst = list(range(1, 10000)) | |
In [24]: def f(): | |
...: for x in arr: | |
...: pass | |
...: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
from pprint import pprint | |
s1 = """ | |
if x == 1: | |
y = 31 | |
elif x == 2: | |
y = 28 | |
elif x == 3: | |
y = 31 |