It's about precision. Each clock in python has a corresponding precision which explains the difference that you are experiencing. Let's see the clock details from a Macbook Pro 2018 with MacOS Mojave. Python3.7 is installed via brew:
In [41]: time.perf_counter_ns()
Out[41]: 10464788941125
In [42]: time.process_time_ns()
Out[42]: 22502272000
In [43]: time.time_ns()
Out[43]: 1545312118561931000
In [44]: time.monotonic_ns()
Out[44]: 10477720411470
In [45]: time.get_clock_info('perf_counter')
Out[45]: namespace(adjustable=False, implementation='mach_absolute_time()', monotonic=True, resolution=1e-09)
In [46]: time.get_clock_info('process_time')
Out[46]: namespace(adjustable=False, implementation='clock_gettime(CLOCK_PROCESS_CPUTIME_ID)', monotonic=True, resolution=1.0000000000000002e-06)
In [47]: time.get_clock_info('time')
Out[47]: namespace(adjustable=True, implementation='clock_gettime(CLOCK_REALTIME)', monotonic=False, resolution=1.0000000000000002e-06)
In [48]: time.get_clock_info('monotonic')
Out[48]: namespace(adjustable=False, implementation='mach_absolute_time()', monotonic=True, resolution=1e-09)
Please pay attention to implementation
and resolution
. Here are the same info but from a VM running on an Ubuntu server:
>>> time.perf_counter_ns()
4094438601446186
>>> time.process_time_ns()
35344006
>>> time.time_ns()
1545312252720125938
>>> time.monotonic_ns()
4094449881239590
>>> time.get_clock_info('perf_counter')
namespace(adjustable=False, implementation='clock_gettime(CLOCK_MONOTONIC)', monotonic=True, resolution=1e-09)
>>> time.get_clock_info('time')
namespace(adjustable=True, implementation='clock_gettime(CLOCK_REALTIME)', monotonic=False, resolution=1e-09)
>>> time.get_clock_info('process_time')
namespace(adjustable=False, implementation='clock_gettime(CLOCK_PROCESS_CPUTIME_ID)', monotonic=True, resolution=1e-09)
>>> time.get_clock_info('monotonic')
namespace(adjustable=False, implementation='clock_gettime(CLOCK_MONOTONIC)', monotonic=True, resolution=1e-09)
As you can see each clock has a different implementation and precision which depends on platform. On MacOS the clock precision for process_time
and time
clocks is set to 1e-06
which is microseconds. That explains the difference.
You can also use time.clock_getres
to get the precision:
In [51]: time.clock_getres(time.CLOCK_REALTIME)
Out[51]: 1.0000000000000002e-06
Further reading: