Created
June 11, 2019 02:55
-
-
Save Coldsp33d/c4a329fd00604e47d513b32e8a25f298 to your computer and use it in GitHub Desktop.
This file contains 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 perfplot | |
import pandas as pd | |
import re | |
p1 = re.compile(r'\D') | |
p2 = re.compile(r'\d+') | |
def eumiro(df): | |
return df.assign( | |
result=df['result'].map(lambda x: x.lstrip('+-').rstrip('aAbBcC'))) | |
def coder375(df): | |
return df.assign( | |
result=df['result'].replace(r'\D', r'', regex=True)) | |
def monkeybutter(df): | |
return df.assign(result=df['result'].map(lambda x: x[1:-1])) | |
def wes(df): | |
return df.assign(result=df['result'].str.lstrip('+-').str.rstrip('aAbBcC')) | |
def cs1(df): | |
return df.assign(result=df['result'].str.replace(r'\D', '')) | |
def cs2_ted(df): | |
# `str.extract` based solution, similar to @Ted Petrou's. so timing together. | |
return df.assign(result=df['result'].str.extract(r'(\d+)', expand=False)) | |
def cs1_listcomp(df): | |
return df.assign(result=[p1.sub('', x) for x in df['result']]) | |
def cs2_listcomp(df): | |
return df.assign(result=[p2.search(x)[0] for x in df['result']]) | |
def cs_eumiro_listcomp(df): | |
return df.assign( | |
result=[x.lstrip('+-').rstrip('aAbBcC') for x in df['result']]) | |
def cs_mb_listcomp(df): | |
return df.assign(result=[x[1:-1] for x in df['result']]) | |
df_ = pd.DataFrame({ | |
'time': ['09:00', '10:00', '11:00', '12:00', '13:00'], | |
'result': ['+52A', '+62B', '+44a', '+30b', '-110a'] | |
}) | |
kernels = [ | |
eumiro, coder375, monkeybutter, wes, cs1, cs2_ted, | |
cs1_listcomp, cs2_listcomp, cs_eumiro_listcomp, cs_mb_listcomp | |
] | |
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, 15)], | |
xlabel='N', | |
logx=True, | |
logy=True, | |
equality_check=lambda x, y: (x == y).all(axis=None),) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment