|
| 1 | +--- |
| 2 | +Title: 'perf_counter()' |
| 3 | +Description: 'Returns a high-resolution timer value, useful for measuring short durations, including time elapsed during sleep.' |
| 4 | +Subjects: |
| 5 | + - 'Code Foundations' |
| 6 | + - 'Computer Science' |
| 7 | +Tags: |
| 8 | + - 'Functions' |
| 9 | + - 'Modules' |
| 10 | + - 'Time' |
| 11 | +CatalogContent: |
| 12 | + - 'learn-python-3' |
| 13 | + - 'paths/computer-science' |
| 14 | +--- |
| 15 | + |
| 16 | +The **`perf_counter()`** function returns a floating-point number representing a high-resolution timer value (in seconds) at the moment of the call, and is commonly used to benchmark code by measuring the time taken to execute a piece of code. |
| 17 | + |
| 18 | +## Syntax |
| 19 | + |
| 20 | +```pseudo |
| 21 | +time.perf_counter() |
| 22 | +``` |
| 23 | + |
| 24 | +**Parameters:** |
| 25 | + |
| 26 | +The function takes no arguments. |
| 27 | + |
| 28 | +**Return value:** |
| 29 | + |
| 30 | +Returns a float representing the current value of a high-resolution performance counter in seconds. |
| 31 | + |
| 32 | +## Example 1 |
| 33 | + |
| 34 | +This example shows how to call `perf_counter()` to get the current high-resolution timer value: |
| 35 | + |
| 36 | +```py |
| 37 | +import time |
| 38 | + |
| 39 | +print(time.perf_counter()) |
| 40 | +``` |
| 41 | + |
| 42 | +A possible output of this code is: |
| 43 | + |
| 44 | +```shell |
| 45 | +5003.666789873 |
| 46 | +``` |
| 47 | + |
| 48 | +Importing `perf_counter()` from `time`: |
| 49 | + |
| 50 | +```py |
| 51 | +from time import perf_counter |
| 52 | + |
| 53 | +print(perf_counter()) |
| 54 | +``` |
| 55 | + |
| 56 | +A possible outcome of this code is: |
| 57 | + |
| 58 | +```shell |
| 59 | +1780743.368374651 |
| 60 | +``` |
| 61 | + |
| 62 | +## Example 2 |
| 63 | + |
| 64 | +Here, two `perf_counter()` calls are placed around [`sleep()`](https://www.codecademy.com/resources/docs/python/time-module/sleep) which adds a 5-second delay in the execution right after the first method call: |
| 65 | + |
| 66 | +```py |
| 67 | +from time import perf_counter, sleep |
| 68 | + |
| 69 | +# record current time before delay |
| 70 | +start = perf_counter() |
| 71 | + |
| 72 | +print('Time before sleep():', start) |
| 73 | + |
| 74 | +sleep(5) # pause for 5 seconds |
| 75 | + |
| 76 | +# record current time after delay |
| 77 | +end = perf_counter() |
| 78 | + |
| 79 | +print('Time after sleep():', end) |
| 80 | + |
| 81 | +# calculate elapsed time between both calls |
| 82 | +elapsed_time = end - start |
| 83 | + |
| 84 | +print('Elapsed time in seconds:', elapsed_time) |
| 85 | +``` |
| 86 | + |
| 87 | +The output of this code is: |
| 88 | + |
| 89 | +```shell |
| 90 | +Time before sleep(): 1781952.36727265 |
| 91 | +Time after sleep(): 1781957.367474534 |
| 92 | +Elapsed time in seconds: 5.000201884191483 |
| 93 | +``` |
| 94 | + |
| 95 | +## Codebyte Example |
| 96 | + |
| 97 | +The following program shows two ways to search for an element in a sorted integer list. Both solutions are written in separate functions, and pairs of `perf_counter()` calls are used to track the time taken to execute each function: |
| 98 | + |
| 99 | +```codebyte/python |
| 100 | +from time import perf_counter |
| 101 | +
|
| 102 | +# linear search: search through every element in the list |
| 103 | +def search_1(nums, target) -> bool: |
| 104 | + for num in nums: |
| 105 | + if (num == target): |
| 106 | + return True |
| 107 | +
|
| 108 | + return False |
| 109 | +
|
| 110 | +# binary search: searching within half of the list using the midpoint |
| 111 | +def search_2(nums, target) -> bool: |
| 112 | + left = 0 |
| 113 | + right = len(nums) - 1 |
| 114 | +
|
| 115 | + while left <= right: |
| 116 | + mid = (left + right) // 2 |
| 117 | +
|
| 118 | + if nums[mid] == target: |
| 119 | + return True |
| 120 | +
|
| 121 | + if nums[mid] < target: |
| 122 | + left = mid + 1 |
| 123 | + else: |
| 124 | + right = mid - 1 |
| 125 | +
|
| 126 | + return False |
| 127 | +
|
| 128 | +# example values |
| 129 | +nums = [-109, -83, -57, -43, -39, -10, -5, -2, 0, 1, 7, 18, 45, 78, 82, 94, 123, 165] # sorted in ascending order |
| 130 | +target = 165 |
| 131 | +
|
| 132 | +# track elapsed time for search_1() |
| 133 | +begin_result1 = perf_counter() |
| 134 | +result1 = search_1(nums, target) |
| 135 | +end_result1 = perf_counter() |
| 136 | +elapsed_result1 = end_result1 - begin_result1 |
| 137 | +
|
| 138 | +# track elapsed time for search_2() |
| 139 | +begin_result2 = perf_counter() |
| 140 | +result2 = search_2(nums, target) |
| 141 | +end_result2 = perf_counter() |
| 142 | +elapsed_result2 = end_result2 - begin_result2 |
| 143 | +
|
| 144 | +print('Time taken for search_1(): {} seconds'.format(elapsed_result1)) |
| 145 | +print('Time taken for search_2(): {} seconds'.format(elapsed_result2)) |
| 146 | +``` |
| 147 | + |
| 148 | +> **Note:** The actual numbers will vary each time you run the code, since they represent the current internal timer value, not absolute wall-clock time. |
| 149 | +
|
| 150 | +Ideally, with a large `nums` list the binary search function should take half as much time as the linear search function. |
0 commit comments