In [58]:
%autosave 20
%matplotlib inline

import operator
import math
from functools import reduce, lru_cache
# import cProfile
# import timeit

import matplotlib.pyplot as plt
import numpy as np
import numba


Autosaving every 20 seconds

In [3]:
N = 8192

In [4]:
%timeit 3 + 5


15.2 ns ± 0.044 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)

In [7]:
%%timeit a = np.random.rand(N)
b = a**2
m = np.median(b)


207 µs ± 13.6 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

Сложение скаляра и вектора


In [13]:
a = np.linspace(0, 1, N)
a += 2

In [14]:
%%timeit l = np.linspace(0, 1, N).tolist()
for i in range(len(l)):
    l[i] += 2


892 µs ± 2.04 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [18]:
%%timeit a = np.linspace(0, 1, N)
for i, x in enumerate(a):
    a[i] = 2 + x


3.63 ms ± 205 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [16]:
%%timeit a = np.linspace(0, 1, N)
a += 2


6.6 µs ± 19 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Сумма элементов вектора


In [32]:
a = np.random.normal(0, 1, N)
l = a.tolist()
%timeit a.sum()
%timeit np.sum(a)
%timeit reduce(lambda x, y: x+y, l)
%timeit reduce(operator.add, l)
%timeit sum(l)


9.99 µs ± 49.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
13.5 µs ± 71.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
1.05 ms ± 3.79 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
520 µs ± 1.77 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
51.6 µs ± 165 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [33]:
%%timeit
s = 0
for x in l:
    s += x


285 µs ± 317 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)

numpy vs math


In [34]:
a = np.random.normal(0, 1, N)
l = a.tolist()
%timeit np.sin(a)
%timeit [math.sin(x) for x in l]
%timeit np.sin(1)
%timeit math.sin(1)


221 µs ± 1.86 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
1.51 ms ± 3.64 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
2.01 µs ± 7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
178 ns ± 1.05 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

Объединение строк


In [42]:
text = ['dsifiafandasadsopjop'] * N
%timeit reduce(operator.add, text)
%timeit ''.join(text)


43.6 ms ± 185 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
81.1 µs ± 407 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [43]:
%%timeit
s = ''
for x in text:
    s += x


807 µs ± 6.64 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Цепная дробь


In [63]:
def golden_ratio_naive(n):
    s = 1
    for _ in range(n):
        s = 1 + 1 / s
    return s

def golden_ratio_float(n):
    s = 1.
    for _ in range(n):
        s = 1. + 1. / s
    return s

@numba.jit
def golden_ratio_numba(n):
    s = 1.
    for _ in range(n):
        s = 1. + 1. / s
    return s

%timeit golden_ratio_naive(100_000)
%timeit golden_ratio_float(100_000)
%timeit golden_ratio_numba(100_000)


10 ms ± 70.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
6.79 ms ± 101 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
788 µs ± 5.6 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [61]:
def decorator(f):
    def g(*args, **kwargs):
        return f(*args, **kwargs) + 1

    return g

@lru_cache(maxsize=10)
@decorator
def f(x):
    print(x)
    return x**2

# g = decorator(f)
# g(2)
# decorator(np.sin)(np.pi/2)

In [62]:
print(f(1))
print(f(1))


1
2
2

In [ ]: