You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 25, 2024. It is now read-only.
Profiling code #186
Try the given code in the JupyterNotebook
def sum_of_lists(N):
total = 0
for i in range(5):
L = [j ^ (j >> i) for j in range(N)]
# j >> i == j // 2 ** i (shift j bits i places to the right)
# j ^ i -> bitwise exclusive or; j's bit doesn't change if i's = 0, changes to complement if i's = 1
total += sum(L)
return total
and run %prun sum_of_lists(10_000_000)
Got result below
14 function calls in 13.066 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
Then try %load_ext line_profiler %lprun -f sum_of_lists sum_of_lists(10_000_000)
Got the result
Timer unit: 1e-06 s
Total time: 19.2983 s
File:
Function: sum_of_lists at line 1
Line # Hits Time Per Hit % Time Line Contents
1 def sum_of_lists(N):
2 1 2.0 2.0 0.0 total = 0
3 6 22.0 3.7 0.0 for i in range(5):
4 5 18459236.0 3691847.2 95.7 L = [j ^ (j >> i) for j in range(N)]
5 # j >> i == j // 2 ** i (shift j bits i places to the right)
6 # j ^ i -> bitwise exclusive or; j's bit doesn't change if i's = 0, changes to complement if i's = 1
7 5 839024.0 167804.8 4.3 total += sum(L)
8 1 1.0 1.0 0.0 return total
The line that takes most time is the looping command: L = [j ^ (j >> i) for j in range(N)]
Improving performance using MPI #194
The code uses the Message Passing Interface (MPI) to accomplish this approximation of π.
run python calc_pi.py -np 10_000_000 -n 1 -r 1
got
pi = 3.1411436 (with 10000000)
1 loops, best of 1: 7.97 sec per loop
numpy is the fastest MPI could work faster when performing parallel tasks.
This file contains hidden or 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
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
None yet
2 participants
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Running the given function without numpy
python -m timeit -n 100 -r 5 -s "from calc_pi import calculate_pi_timeit" "calculate_pi_timeit(10_000)()"get
After use numpy functions, run:
python -m timeit -n 100 -r 5 -s "from calc_pi_np import calculate_pi_timeit" "calculate_pi_timeit(10_000)()"get
numpy makes the code faster.