Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a much shorter time to measure current cpu frequency #210

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions cpuinfo/cpuinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import platform
import multiprocessing
import ctypes
from time import perf_counter


CAN_CALL_CPUID_IN_SUBPROCESS = True
Expand Down Expand Up @@ -1517,19 +1518,31 @@ def new_func():
retval = get_ticks_x86_64
return retval

def get_raw_hz(self):
from time import sleep
def get_raw_hz(self, measurement_time=0.01):
from time import sleep, perf_counter_ns

ticks_fn = self.get_ticks_func()

# when testing, more consistent results between different
# measurement times were obtained by measuring the actual time
# spend, not just using the user-provided value. For some reason,
# it was also better to have the time around the ticks_func / sleep calls
# and then account for the time spend in the ticks_func
ticks_start = perf_counter_ns()
ticks_fn.func()
ticks_fn_time = perf_counter_ns() - ticks_start

t0 = perf_counter_ns()
start = ticks_fn.func()
sleep(1)
sleep(measurement_time)
end = ticks_fn.func()
t1 = perf_counter_ns()
measurement_time = (t1 - t0) - 2 * ticks_fn_time

ticks = (end - start)
frequency = 1e9 * (end - start) / measurement_time
ticks_fn.free()

return ticks
return frequency

def _get_cpu_info_from_cpuid_actual():
'''
Expand Down