Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import pandas as pd
import numpy as np
import perfplot
import numba
def with_df_loc(df):
df.at[0, 'b'] = 5
for i in range(1, len(df)):
df.at[i, 'b'] = (df.at[i - 1, 'a'] + df.at[i - 1, 'b']) / 2
return df
@numba.njit
def func(a, b_0=5):
n = len(a)
b = np.full(n, b_0, dtype=np.float64)
for i in range(1, n):
b[i] = (b[i - 1] + a[i - 1]) / 2
return b
def with_numba(df):
df['b'] = func(df['a'].to_numpy())
return df
df = pd.DataFrame([1, 6, 2, 8], columns=['a'])
kernels = [with_df_loc, with_numba]
perfplot.show(
setup=lambda n: pd.concat([df] * n, ignore_index=True),
kernels=kernels,
labels=[k.__name__ for k in kernels],
n_range=[2**k for k in range(1, 12)],
xlabel='N',
logx=True,
logy=True,
equality_check=np.allclose)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment