Python 计时器功能:测量经过的时间(示例)

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

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

使用方法 Python 时间结构?

时间结构 Python 由 time.struct_time 对象表示。语法 Python 时间结构如下所示:-

句法

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_年份 0000、2020、…、9999
1 tm_mon 1,2、3、12、…XNUMX
2 tm_mday 1,2,3,4,….5
3 tm_小时 0,1,2,3、4、23、…XNUMX
4 tm_min 0,1,2,3、59、XNUMX、XNUMX….、XNUMX
5 时间秒 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 中, Monday 表示为 0。

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

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

使用方法 Python 時代?

一个时代 Python 被定义为选定为特定时代的开始的时间实例。要获取有关 Python 纪元,您需要将零作为参数传递给 time.gmtime 函数。纪元表示为时间 00:00:00 UTC as of 1970, 1st January, 也可以表示为 1970-01-01T00:00:00ZISO8601.

跟随 Python 代码展示了 Python 时间纪元如下所示:-

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 模块返回自上次定义的纪元以来经过的秒数。它以浮点数据类型的形式返回秒数。

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

语法:

time.time()

以下代码显示如何在 Python -

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

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

计算挂钟时间的程序如下所示:

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 时间.ctime()?

ctime 函数 Python time 模块以秒为参数,以纪元形式传递,并作为输出,提供本地时间 字符串数据类型.

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

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

ctime() 函数的语法 Python 时间模块如下所示:

语法:

time.ctime()

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

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模块,使用time方法初始化变量start_time_guru99,同样初始化变量end_time_guru99。

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

使用方法 Python 计时器的 time.sleep() 是什么?

睡眠功能 Python time 模块有助于减慢程序的执行速度。它会暂停程序执行几秒钟,并将其作为参数传递给 sleep 函数。

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

sleep 函数的语法 Python 时间模块如下所示:-

语法:

Time.sleep(10)

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

使用方法 Python 时间。 Python time.gmtime()?

的 time 模块中的 gmtime() 函数 Python 以 epoch 完成后经过的秒数作为参数。该函数以 struct_time 或 UTC 格式返回输出。此处,UTC 表示协调世界时。

gmtime() 函数的语法如下:-

句法

time.gmtime(argument)

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

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 作为参数传递到 gmtime 函数中,该函数向最终用户提供结构化时间格式作为输出。

如何在 Python?

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

时钟功能的语法如下所示:-

语法:

time.clock()

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

如何在 Python?

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

下面的例子展示了线程时间函数的应用。

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

上述代码返回在名为线程示例的线程中花费的开始时间和结束时间之间的时间差,它返回的差值分别为 1.029。

如何在 Python?

时间模块中的进程时间函数 Python 以秒的小数部分和浮点值形式返回时间的参考。该函数给出系统时间与 CPU 时间的当前进度之和。

此函数显示与 thread_time 函数类似的属性,即不包括 time.sleep function() 所花费的时间。它还创建基于特定进程的引用。由于此属性,承担了两个连续引用之间的时间差。

以下代码有助于描述处理时间函数的用法,如下所示:-

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?

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.

上面的代码中,我们用一个简单的循环来验证函数性能计数器记录从开始到结束时间所花费的时间。作为输出展示,计时器提供了高精度的输出。

如何检查时区 Python?

In Python,时间模块的时间函数下有两个属性,为最终用户提供与时区相关的信息。

  • 第一个是time.timezone属性,第二个是time.tzname。
  • time.timezone 返回非DST或UTC格式的当地时区的偏移量。
  • 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 可以使用性能计数器、进程计时器等计时器以及使用上面描述的时间函数从执行角度评估现有代码的性能。

总结

其他重要事项 Python 定时器模块:

Python 提供了几个有助于时间管理的模块,也有助于代码分析。其他模块可用于对用 Python 如下面所述:

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