Skip to content

Instantly share code, notes, and snippets.

@Coldsp33d
Last active December 26, 2020 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Coldsp33d/cc71e95f7edda25ee798c99195d245ab to your computer and use it in GitHub Desktop.
Save Coldsp33d/cc71e95f7edda25ee798c99195d245ab to your computer and use it in GitHub Desktop.
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