Skip to content

Commit 7f3dd63

Browse files
authored
Add unit example and example page (#293)
1 parent 534511c commit 7f3dd63

File tree

5 files changed

+133
-1
lines changed

5 files changed

+133
-1
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ index.html
2424
tests/coverage.xml
2525
tests/htmlcov
2626
wheelhouse
27+
profile_output*
28+
*.egg-info/

docs/source/index.rst

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
auto/line_profiler.explicit_profiler
1212
auto/kernprof
1313

14+
manual/examples/index
15+
1416

1517
Indices and tables
1618
==================
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
Timing Units
2+
------------
3+
4+
This example demonstrates how you can change the units in which the time is
5+
reported.
6+
7+
Write the following demo script to disk
8+
9+
.. code:: bash
10+
11+
echo "if 1:
12+
from line_profiler import profile
13+
14+
@profile
15+
def is_prime(n):
16+
max_val = n ** 0.5
17+
stop = int(max_val + 1)
18+
for i in range(2, stop):
19+
if n % i == 0:
20+
return False
21+
return True
22+
23+
24+
def find_primes(size):
25+
primes = []
26+
for n in range(size):
27+
flag = is_prime(n)
28+
if flag:
29+
primes.append(n)
30+
return primes
31+
32+
33+
def main():
34+
print('start calculating')
35+
primes = find_primes(10)
36+
primes = find_primes(1000)
37+
primes = find_primes(100000)
38+
print(f'done calculating. Found {len(primes)} primes.')
39+
40+
41+
if __name__ == '__main__':
42+
main()
43+
" > script.py
44+
45+
46+
Run the script with line profiling on. To change the unit in which time is
47+
reported use the ``--unit`` command line argument. The following example shows
48+
4 variants:
49+
50+
.. code:: bash
51+
LINE_PROFILE=1 python script.py
52+
53+
# Use different values for the unit report
54+
python -m line_profiler -rtmz --unit 1 profile_output.lprof
55+
python -m line_profiler -rtmz --unit 1e-3 profile_output.lprof
56+
python -m line_profiler -rtmz --unit 1e-6 profile_output.lprof
57+
python -m line_profiler -rtmz --unit 1e-9 profile_output.lprof
58+
59+
60+
You will notice the relevant difference in the output lines:
61+
62+
63+
.. code::
64+
65+
66+
==============
67+
unit 1 variant
68+
==============
69+
70+
Timer unit: 1 s
71+
72+
...
73+
74+
6 101010 0.0 0.0 3.6 max_val = n ** 0.5
75+
7 101010 0.1 0.0 4.0 stop = int(max_val + 1)
76+
77+
...
78+
79+
=================
80+
unit 1e-3 variant
81+
=================
82+
83+
Timer unit: 0.001 s
84+
85+
...
86+
87+
6 101010 46.6 0.0 3.6 max_val = n ** 0.5
88+
7 101010 51.5 0.0 4.0 stop = int(max_val + 1)
89+
90+
...
91+
92+
=================
93+
unit 1e-6 variant
94+
=================
95+
96+
Timer unit: 1e-06 s
97+
98+
...
99+
100+
6 101010 46558.2 0.5 3.6 max_val = n ** 0.5
101+
7 101010 51491.7 0.5 4.0 stop = int(max_val + 1)
102+
103+
...
104+
105+
=================
106+
unit 1e-9 variant
107+
=================
108+
109+
Timer unit: 1e-09 s
110+
111+
...
112+
113+
6 101010 46558246.0 460.9 3.6 max_val = n ** 0.5
114+
7 101010 51491716.0 509.8 4.0 stop = int(max_val + 1)
115+
116+
...

docs/source/manual/examples/index.rst

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Examples
2+
--------
3+
4+
Examples of line profiler usage:
5+
6+
+ `Basic Usage <../../index.html#line-profiler-basic-usage>`_
7+
8+
+ `Auto Profiling <../../auto/line_profiler.autoprofile.html#auto-profiling>`_
9+
10+
+ `Explicit Profiler <../../auto/line_profiler.explicit_profiler.html#module-line_profiler.explicit_profiler>`_
11+
12+
+ `Timing Units <example_units.rst>`_

line_profiler/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def main():
224224
* `torch.profiler <https://pytorch.org/docs/stable/profiler.html>`_ tools for profiling torch code.
225225
226226
227-
.... todo: give more details on exact limitations.
227+
.. .. todo: give more details on exact limitations.
228228
229229
"""
230230
# Note: there are better ways to generate primes

0 commit comments

Comments
 (0)