Skip to content

Instantly share code, notes, and snippets.

View juanarrivillaga's full-sized avatar

Juan juanarrivillaga

View GitHub Profile
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 = [
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 = []
@juanarrivillaga
juanarrivillaga / listcomp_vs_forloop_vs_numpy.py
Last active October 4, 2018 08:40
some basic comparisons of numpy arrays vs vanilla lists
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
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:
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]
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):
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)
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)
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
...:
import timeit
from pprint import pprint
s1 = """
if x == 1:
y = 31
elif x == 2:
y = 28
elif x == 3:
y = 31