Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
39ebc38d
Commit
39ebc38d
authored
Jun 15, 2018
by
scoder
Committed by
GitHub
Jun 15, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2355 from gabrieldemarmiesse/test_profiling_tutorial_1
Added tests to "profiling tutorial" part 1.
parents
80b910e4
8524b096
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
24 additions
and
26 deletions
+24
-26
docs/examples/tutorial/profiling_tutorial/calc_pi.py
docs/examples/tutorial/profiling_tutorial/calc_pi.py
+10
-0
docs/examples/tutorial/profiling_tutorial/profile.py
docs/examples/tutorial/profiling_tutorial/profile.py
+10
-0
docs/src/tutorial/profiling_tutorial.rst
docs/src/tutorial/profiling_tutorial.rst
+4
-26
No files found.
docs/examples/tutorial/profiling_tutorial/calc_pi.py
0 → 100644
View file @
39ebc38d
# calc_pi.py
def
recip_square
(
i
):
return
1.
/
i
**
2
def
approx_pi
(
n
=
10000000
):
val
=
0.
for
k
in
range
(
1
,
n
+
1
):
val
+=
recip_square
(
k
)
return
(
6
*
val
)
**
.
5
docs/examples/tutorial/profiling_tutorial/profile.py
0 → 100644
View file @
39ebc38d
# profile.py
import
pstats
,
cProfile
import
calc_pi
cProfile
.
runctx
(
"calc_pi.approx_pi()"
,
globals
(),
locals
(),
"Profile.prof"
)
s
=
pstats
.
Stats
(
"Profile.prof"
)
s
.
strip_dirs
().
sort_stats
(
"time"
).
print_stats
()
docs/src/tutorial/profiling_tutorial.rst
View file @
39ebc38d
...
@@ -125,20 +125,9 @@ relation we want to use has been proven by Euler in 1735 and is known as the
...
@@ -125,20 +125,9 @@ relation we want to use has been proven by Euler in 1735 and is known as the
\frac{1}{2^2} + \dots + \frac{1}{k^2} \big) \approx
\frac{1}{2^2} + \dots + \frac{1}{k^2} \big) \approx
6 \big( \frac{1}{1^2} + \frac{1}{2^2} + \dots + \frac{1}{n^2} \big)
6 \big( \frac{1}{1^2} + \frac{1}{2^2} + \dots + \frac{1}{n^2} \big)
A simple Python code for evaluating the truncated sum looks like this:
:
A simple Python code for evaluating the truncated sum looks like this:
#!/usr/bin/env python
.. literalinclude:: ../../examples/tutorial/profiling_tutorial/calc_pi.py
# encoding: utf-8
# filename: calc_pi.py
def recip_square(i):
return 1./i**2
def approx_pi(n=10000000):
val = 0.
for k in range(1,n+1):
val += recip_square(k)
return (6 * val)**.5
On my box, this needs approximately 4 seconds to run the function with the
On my box, this needs approximately 4 seconds to run the function with the
default n. The higher we choose n, the better will be the approximation for
default n. The higher we choose n, the better will be the approximation for
...
@@ -147,20 +136,9 @@ places to optimize this code. But remember the golden rule of optimization:
...
@@ -147,20 +136,9 @@ places to optimize this code. But remember the golden rule of optimization:
Never optimize without having profiled. Let me repeat this: **Never** optimize
Never optimize without having profiled. Let me repeat this: **Never** optimize
without having profiled your code. Your thoughts about which part of your
without having profiled your code. Your thoughts about which part of your
code takes too much time are wrong. At least, mine are always wrong. So let's
code takes too much time are wrong. At least, mine are always wrong. So let's
write a short script to profile our code:
:
write a short script to profile our code:
#!/usr/bin/env python
.. literalinclude:: ../../examples/tutorial/profiling_tutorial/profile.py
# encoding: utf-8
# filename: profile.py
import pstats, cProfile
import calc_pi
cProfile.runctx("calc_pi.approx_pi()", globals(), locals(), "Profile.prof")
s = pstats.Stats("Profile.prof")
s.strip_dirs().sort_stats("time").print_stats()
Running this on my box gives the following output:
Running this on my box gives the following output:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment