-
-
Save kongscn/19d28be29215f11c2be84e8004eec5b0 to your computer and use it in GitHub Desktop.
write fast python blog https://kongs.medium.com/writing-fast-python-5c15251124d3
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
%%time | |
cs = [] | |
for _idx, row in df.iterrows(): | |
c2 = row['a']**2 + row['b']**2 | |
cs.append(math.sqrt(c2)) | |
# Wall time: 667 ms |
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
def foo(x): | |
a = bar1(x) | |
b = bar2(a) | |
c = bar3(b) | |
return c |
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
%load_ext line_profiler | |
%lprun -f foo foo(x) |
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
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) |
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
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 |
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
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 |
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
%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