Skip to content

Instantly share code, notes, and snippets.

@kongscn
Last active December 8, 2020 03:54
Show Gist options
  • Save kongscn/19d28be29215f11c2be84e8004eec5b0 to your computer and use it in GitHub Desktop.
Save kongscn/19d28be29215f11c2be84e8004eec5b0 to your computer and use it in GitHub Desktop.
%%time
cs = []
for _idx, row in df.iterrows():
c2 = row['a']**2 + row['b']**2
cs.append(math.sqrt(c2))
# Wall time: 667 ms
def foo(x):
a = bar1(x)
b = bar2(a)
c = bar3(b)
return c
%load_ext line_profiler
%lprun -f foo foo(x)
def pt1(df):
cs = []
for a, b in df.itertuples(index=False):
c2 = a**2 + b**2
cs.append(math.sqrt(c2))
return cs
%timeit _ = pt1(df)
# 10.8 ms ± 173 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Timer unit: 1e-07 s
Total time: 1.42588 s
File: <ipython-input-2-33cd5359c288>
Function: foo at line 1
Line # Hits Time Per Hit % Time Line Contents
==============================================================
1 def foo(x):
2 1 5139999.0 5139999.0 36.0 a = bar1(x)
3 1 7074258.0 7074258.0 49.6 b = bar2(a)
4 1 2044485.0 2044485.0 14.3 c = bar3(b)
5 1 26.0 26.0 0.0 return c
Timer unit: 1e-07 s
Total time: 1.9012 s
File: <ipython-input-36-6fbec21419d8>
Function: pt0 at line 1
Line # Hits Time Per Hit % Time Line Contents
==============================================================
1 def pt0(df):
2 1 21.0 21.0 0.0 cs = []
3 10001 12287695.0 1228.6 64.6 for _idx, row in df.iterrows():
4 10000 6535856.0 653.6 34.4 c2 = row['a']**2 + row['b']**2
5 10000 188400.0 18.8 1.0 cs.append(math.sqrt(c2))
6 1 5.0 5.0 0.0 return cs
%timeit _ = np.sqrt(df['a'] ** 2 + df['b'] ** 2)
# 481 µs ± 9.35 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
## if it's still too slow and we can use the numpy array as a result:
dfv = df.values
%timeit _ = np.sqrt(dfv[:, 0]**2 + dfv[:, 1]**2)
# 45.7 µs ± 179 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment