|
| 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 | + ... |
0 commit comments