Python Timer 函数:使用示例测量经过时间

Python 提供了一个库,可以帮助以多种方式理解和读取时间信息。在 Python 中,这个库称为 time 模块,该模块提供时间和日期对象来执行任何与时间相关的操作。这些操作不操作时间戳或字符串;而是操作对象。

此外,Python time 模块提供了几个函数,可以帮助程序员在 Python 中编写与时间管理相关的任务。time 模块可以部署或用于现有的 Python 代码中,以记录 Python 代码的性能,这使得程序员能够分析代码的性能。

如何使用 Python time 结构?

Python 中的 time 结构由 time.struct_time 对象表示。Python time 结构的语法如下所示:

语法

Time.struct_time(tm_year=2020, tm_mon=4,tm_mday=22,tm_hour=20,tm_min=20,tm_sec=24,tm_wday=4,tm_yday=200,tm_isdst=1)

time.struct_time 的结构或顺序如下:

索引 属性
0 tm_year 0000, 2020, …,9999
1 tm_mon 1,2, 3,…12
2 tm_mday 1,2,3,4, 5,….31
3 tm_hour 0,1,2,3, 4,…23
4 tm_min 0,1,2,3….,59
5 tm_sec 0,1,2,3…,61
6 tm_wday 0,1,2,3,4,5,6
7 tm_yday 1,2,3,4,…366
8 tm_isdist -1,0,1

在参数中,tm_wday,星期一表示为 0。

time.struct_time 值对象可以使用属性和索引来表示。

asctime() 函数有助于将 time.struct_time 转换为人类可读的字符串格式。

如何使用 Python time epoch?

Python 中的 epoch 定义为被选为特定时代开始的时间点。要获取 Python epoch 的信息,您需要将零作为参数传递给 time.gmtime 函数。epoch 表示为 00:00:00 UTC of 1970, 1st January,它也表示为 1970-01-01T00:00:00ZISO8601

以下 Python 代码展示了 Python time epoch 的应用,如下所示:

Python 代码

import time
EpochExampleGuru99=time.gmtime(0)
print("The Python epoch is",EpochExampleGuru99)
print("The Python epoch year is ",EpochExampleGuru99.tm_year)
print("The Python epoch month is ",EpochExampleGuru99.tm_mon)
print("The Python epoch day is ", EpochExampleGuru99.tm_mday)

输出

The Python epoch is time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

The Python epoch year is 1970
The Python epoch month is 1
The Python epoch day is 1

如何使用 Python time.time() 作为计时器?

Python time 模块中的 time 函数返回自上一个定义的 epoch 以来经过的秒数。它以浮点数据类型的形式返回秒数。

Python time.time() 函数的语法如下所示:

语法

time.time()

以下代码展示了如何在 Python 中使用 time 函数:

Python 代码

import time
print("The example shows the seconds by utilizing the time function : ", time.time())

输出

The example shows the seconds by utilizing the time function:1643044348.60835

上面的示例显示了浮点值和秒数。time 函数可用于通过获取两个参考点来计算经过的墙壁时钟时间。

经过墙壁时钟时间的程序如下所示

Python 代码

import time
start_time_Guru99 = time.time()
print("Time elapsed after some level wait...")
print("The start time is", start_time_Guru99)
time.sleep(1)
end_time_Guru99 = time.time()
print("The end time is", end_time_Guru99)
print("Time elapsed in this example code: ", end_time_Guru99 - start_time_Guru99)

输出

Time elapsed after some level of wait...
The start time is 1643044897.5446985
The end time is 1643044898.545785
Time elapsed in this example code: 1.0010864734649658

如何使用 Python time.ctime()?

Python time 模块中的 ctime 函数接受一个秒数作为参数,该秒数作为 epoch 传递,并输出为字符串数据类型的本地时间。

自上一个 epoch 以来经过的秒数成为 ctime 函数的输入。

time 函数也可以用作 ctime 函数的输入或参数。该函数的作用是输出可理解的格式或人类可理解的格式。

Python time 模块的 ctime() 函数语法如下所示

语法

time.ctime()

以下 Python 代码有助于说明 Python 模块中 ctime() 函数的示例。

Python 代码

import time
start_time_Guru99 = time.time()
print("Time elapsed after some level wait...")
print("The start time is",start_time_Guru99)
print("The start time in human form",time.ctime(start_time_Guru99))
time.sleep(1)
end_time_Guru99 = time.time()
print(time.ctime(time.time()))
print("The end time is",end_time_Guru99)
print("The start time in human form",time.ctime(end_time_Guru99))
print("Time elapsed in this example code: ",end_time_Guru99 - start_time_Guru99)

输出

Time elapsed after some level of wait...
The start time is 1643045765.633842
The start time in human form Mon Jan 24 17:36:05 2022
Mon Jan 24 17:36:06 2022
The end time is 1643045766.634578
The start time in human form Mon Jan 24 17:36:06 2022
Time elapsed in this example code:  1.0007359981536865

上面的代码以导入 time 模块开始。变量 start_time_guru99 使用 time 方法初始化,同样,变量 end_time_guru99 也被初始化。

然后将这些变量转换为 ctime 格式,将时间格式转换为字符串格式。此 Python 代码计算两个初始化变量的差值以得出经过的时间值。上面的输出显示了人类可读的字符串值。它还以浮点格式显示了差值。

如何使用 Python time.sleep() 作为计时器?

Python time 模块中提供的 sleep 函数有助于减慢程序的执行速度。它会暂停程序执行几秒钟,这些秒数作为参数传递到 sleep 函数中。

它还接受浮点数作为输入,以获得更精确的睡眠时间或暂停当前执行线程。

Python time 模块中 sleep 函数的语法如下所示:

语法

Time.sleep(10)

sleep 函数的应用扩展到几种编程情况。一种情况是数据库提交,另一种情况是等待文件完成。

以下代码显示了 time 模块的 sleep 函数,如下所示:

Python 代码

import time
start_time_Guru99 = time.time()
print("Time elapsed after some level wait...")
print("The start time is", start_time_Guru99)
print("The start time in human form", time.ctime(start_time_Guru99))
print("Before calling the method")
time.sleep(1)
time.sleep(10.5)
print("after calling the sleep method")
end_time_Guru99 = time.time()
print(time.ctime(time.time()))
print("The end time is", end_time_Guru99)
print("The start time in human form", time.ctime(end_time_Guru99))
print("Time elapsed in this example code: ", end_time_Guru99 - start_time_Guru99)

输出

Time elapsed after some level of wait...
The start time is 1643046760.163671
The start time in the human form Mon Jan 24 17:52:40 2022
Before calling the method
after calling the sleep method
Mon Jan 24 17:52:51 2022
The end time is 1643046771.6733172
The start time in human form Mon Jan 24 17:52:51 2022
Time elapsed in this example code:  11.50964617729187

如何使用 time.gmtime()?

time 模块中的 gmtime() 函数接受一个自 epoch 完成以来的秒数作为参数。该函数以 struct_time 或 UTC 格式返回输出。此处,UTC 是指协调世界时。

gmtime() 函数的语法如下:

语法

time.gmtime(argument)

以下代码是关于如何使用 Python 的 gmtime() 函数的示例,如下所示:

Python 代码

import time
result = time.gmtime(time.time())
print("The structure format of time is as follows")
print(result)
print("Year in structured format is represented as", result.tm_year)
print("Hour in structured format is represented as", result.tm_hour)

输出

The structure format of time is as follows
time.struct_time(tm_year=2022, tm_mon=1, tm_mday=25, tm_hour=5, tm_min=48, tm_sec=27, tm_wday=1, tm_yday=25, tm_isdst=0)

 • Year in structured format is represented as 2022
 • Hour in structured format is represented as 5

在上面的代码中,Python time 模块的 time 函数作为参数传递到 gmtime 函数中,该函数向最终用户提供结构化时间格式作为输出。

如何在 Python 中使用 time.clock() 函数?

Python time 模块中的 clock 函数将处理时间作为输出返回给最终用户。该函数的主要作用是促进基准测试和性能测试。该函数提供 Python 代码段完成执行所花费的准确时间。该函数提供的输出比其他与时间相关的函数更准确。

clock 函数的语法如下所示:

语法

time.clock()

由于 clock 函数已从 Python 3 版本中弃用,因此程序员可以使用 time.perf_counter() 函数和 time.process_time() 函数来评估他们开发的程序的性能。

如何在 Python 中使用 time.Thread_time 函数?

Python time 模块的 thread_time 函数给出主动运行线程的系统时间和 CPU 时间之和。该函数返回一个浮点值,并且不包括代码在调用 sleep 函数时花费的时间。该函数仅用于特定线程,并且可用于记录两个参考点之间的时间差。

以下示例展示了 thread time 函数的应用。

Python 代码

import time
import threading
start_time1 = None
end_time1 = None
def threadexample1():
    global start_time1, end_time1
start_time1 = time.thread_time()
ExecutionLoop = 0
while ExecutionLoop & lt;
10000000:
    pass
ExecutionLoop += 1
end_time1 = time.thread_time()
threadTarget = threading.Thread(target = threadexample1, args = ())
threadTarget.start()
threadTarget.join()
print("The time spent in thread is {}".format(end_time1 - start_time1))

输出

The time spent in thread is 1.029076937

上面的代码返回名为 thread example 的线程中花费的时间的开始和结束时间之间的差值,它分别返回 1.029 作为差值。

如何在 Python 中使用 time.Process_time() 函数?

Python time 模块中的 process time 函数以小数秒和浮点值的形式返回时间的参考。该函数给出系统时间和当前 CPU 时间的总和。

此函数显示了与 thread_time 函数相似的属性,即不包括在 time.sleep 函数() 中花费的时间。它还创建了一个基于特定进程的参考。由于此属性,将计算两个连续参考之间的时间差。

以下代码有助于描述 process time 函数的用法,如下所示:

Python 代码

from time
import process_time, sleep
g = 20
start_time = process_time() & nbsp;
for i in range(g):
    print(i)
end_time = process_time()
print("The end time and start time are as follows:", end_time, start_time)
print("Elapsed time in seconds:", end_time - start_time)

输出

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
The end time and start time are as follows: 0.059007366 0.058789647
Elapsed time in seconds: 0.00021771899999999816

上面的代码记录了开始和结束时间之间的时长。在开始和结束时间差之间,进程代码的执行被记录为一个进程,并成为经过时间的一部分。

如何在 Python 中使用 time.Perf_counter() 函数?

Perf_counter 函数提供高精度或准确的时间值。perf_counter 函数在两个已建立的参考点(开始时间和结束时间)之间提供准确或精确的时间。

以下代码有助于描述性能计数器,如下所示:

Python 代码

from time
import perf_counter, sleep
g = 6
start_time = perf_counter() & nbsp;
print("the timer started at", start_time) & nbsp; & nbsp;
for i in range(g):
    sleep(1)
end_time = perf_counter()
print("the timer ended at", end_time)
print("Time elapsed in seconds as recorded by performance counter:", end_time - start_time)

输出

the timer started at 6967694.757714532
the timer ended at 6967700.803981042
Time elapsed in seconds as recorded by performance counter: 6.046266509220004.

在上面的代码中,应用了一个简单的循环来验证 perf_counter 函数记录开始和结束时间之间的时间。如输出所示,计时器提供了高精度的输出。

如何在 Python 中检查时区?

在 Python 中,time 模块的 time 函数下有两个属性为最终用户提供有关时区的信息。

  • 第一个是 time.timezone 属性,第二个是 time.tzname。
  • time.timezone 以 UTC 格式返回非夏令时(DST)或本地时区的偏移量。
  • time.tzname 返回一个包含 DST 和非 DST 或本地时区的元组。

time.timezone 的语法如下所示:

语法

time.timezone

time.tzname 的语法如下所示:

语法

time.tzname

以下代码将展示如何在 Python 中使用时区属性:

Python 代码

import time
print("time zone in non-DST format is:",time.timezone)
print("time zone in DST and non-DST format is:",time.tzname)

输出

time zone in non-DST format is: 0
time zone in DST and non-DST format is: ('UTC', 'UTC')

如何开发一个基本的 Python 计时器?

Python 计时器定义为一个库或类,有助于管理代码的时间复杂度。计时器能够在现有的代码片段中创建一个系统,并检查现有代码需要多长时间来完成任务。

如上所述,Python 计时器(如 perf_counter、process_timer 和 time 函数)可用于从执行角度评估现有代码的性能。

摘要

其他重要的 Python 计时器模块

Python 提供了几个模块,有助于时间管理以及代码分析。可用于 Python 代码的时间和精力分析的其他模块如下:

  • Term down:这是一个高级的 Python 计时器,它使用不同的 ASCII 字符。它们用于生成简单的倒计时计时器。
  • MobTimer:Python:这是 Python 中的另一个基于 GUI 的计时器,启动后,它会为最终用户提供具有多个选项的全屏计时器。
  • Ctimer:这是一个 Python 中的计时器模块,精度高达纳秒。它利用 C 语言 API 精确记录时间。