Python模块操作:time—Clock Time(一)

简介: Python模块操作:time—Clock Time(一)

Python模块操作:time—Clock Time
目的:操作时钟的函数
时间模块提供几种不同类型时钟的访问途径,每种都用于不同的目的。标准系统调用像time()报告系统"挂钟"时间。monotonic()时钟函数被用来测量在长期进程中经过的时间,因为它确保时间永不往回走,即使系统时间被改变。对于性能测试,perf_counter()函数提供获取具有最高解析度的时钟访问,使短时间测量更精确。CPU时间通过clock()函数可以获取,并且process_time()函数返回处理器时间和系统时间的组合。

注意
这些实现公开了操作时间和日期的C库函数。由于它们绑定到底层C实现,因此一些细节(例如时间纪元的开始和支持的最大日期值)是限于特定的平台。可以参照库文档了解全部的细节。

比较的时钟
实现的细节根据平台而变化。使用get_clock_info()访问有关当前实现的基本信息,包括时钟的分辨率。

import textwrap
import time

available_clocks = [
    ('monotonic', time.monotonic),
    ('perf_counter', time.perf_counter),
    ('process_time', time.process_time),
    ('time', time.time),
]

for clock_name, func in available_clocks:
    print(textwrap.dedent('''\
    {
   name}:
        adjustable : {
   info.adjustable}
        implementation: {
   info.implementation}
        monotonic: {
   info.monotonic}
        resolution: {
   info.resolution}
        current: {
   current}
        ''').format(
        name=clock_name,
        info=time.get_clock_info(clock_name),
        current=func()
    ))

以下是macOS系统的输出,展示了monotonic和perf_counter时钟的执行是使用相同的系统调用。

monotonic:
    adjustable : False
    implementation: mach_absolute_time()
    monotonic: True
    resolution: 1e-09
    current: 0.94656131

perf_counter:
    adjustable : False
    implementation: mach_absolute_time()
    monotonic: True
    resolution: 1e-09
    current: 0.946618741

process_time:
    adjustable : False
    implementation: clock_gettime(CLOCK_PROCESS_CPUTIME_ID)
    monotonic: True
    resolution: 1.0000000000000002e-06
    current: 0.824873

time:
    adjustable : True
    implementation: clock_gettime(CLOCK_REALTIME)
    monotonic: False
    resolution: 1.0000000000000002e-06
    current: 1671860901.9184608


Process finished with exit code 0

win10系统的输出结果如下图:

image.png

挂钟时间
时间模块的一个核心函数就是time(),这个函数返回纪元开始浮点类型的秒数值。

import time
print('the time is: ',  time.time())

纪元是时间测量的起始,对于Unix系统是0:00, 1970年1月1日。尽管值总是浮点型的,实际的精度还有赖于平台。

macOS系统的输出结果如下:

/usr/local/bin/python3.9 /Users/bruce_liu/wall_clock_time.py
the time is:  1671863148.7370439

win10系统的输出结果如下图:

image.png

当日期用于存储和比较的时候,浮点表示是有用的,但是在生成人类可读的表示来说,用处不大。

对于记录或者打印时间ctime()可能更有用。

import time
print('The time is    :', time.ctime())
later = time.time() + 15
print('15 secs from now: ', time.ctime(later))

第2个print()函数在这个例子里的调用表明如何使用ctime()格式化一个时间值而不是当前时间。

macOS系统运行结果如下:

The time is    : Sat Dec 24 14:52:24 2022
15 secs from now:  Sat Dec 24 14:52:39 2022

Process finished with exit code 0

单调的时钟
因为time()函数查看系统时间,并且系统时钟可以由用户或者系统服务修改,以便在多台计算机上同步时钟,重复地调用time()函数可能产生向前和向后的值。当试图测量时间段或者用那些时间计算的时候,这可能导致不期望的行为。为了避免那些情形,要通过使用monotonic()函数,这个函数返回值总是向前的。

import time

start = time.monotonic()
time.sleep(0.1)
end = time.monotonic()
print('start :  {:>9.2f}'.format(start))
print('end   :  {:>9.2f}'.format(end))
print('span  :  {:>9.2f}'.format(end - start))

macOS系统运行结果如下:

start :       0.45
end   :       0.56
span  :       0.10

win10系统运行结果如下图:

image.png

接下文 Python模块操作:time—Clock Time(二)https://developer.aliyun.com/article/1618982

相关文章
|
22小时前
|
存储 iOS开发 MacOS
Python模块操作:time—Clock Time(二)
Python模块操作:time—Clock Time(二)
|
11月前
Time
Time
148 0
函数 os.time
函数 os.time
171 0
clock函数
clock函数
75 0
|
开发者 Python
Time 模块的使用 | 学习笔记
快速学习 Time 模块的使用
|
Python
time模块 - 日期转换
==== 模块名称:time ==== 一种获取当前时间,以及时间格式化的模块 time模块在Python原生安装中就存在,直接使用即可,无需额外的安装操作 == 导入方式:import time == # -*- coding: utf-8 -*- import time import locale 设置本地语言类型为中文 locale.
959 0
|
Unix
TIME
1168 0