Table combing and analysis python built in time module can be understood after reading it

  • 2021-12-11 07:52:36
  • OfStack

Directory Parameterless Function Time Zone Concept struct_timetime. strftime (format [, t])

Parameterless function

First explain the timestamp, the so-called timestamp, that is, the number of seconds elapsed since 00:00:00 on January 1, 1970, and then you can understand the following function. The following code defaults

from time import *

implementation monotonic adjustable resolution
'time' GetSystemTimeAsFileTime() False True 0.015625
'thread_time' GetThreadTimes() True False 1e-07
'process_time' GetProcessTimes() True False 1e-07
'monotonic' GetTickCount64() True False 0.015625
'perf_counter' QueryPerformanceCounter() True False 1e-07

Of the above five sets of functions, only the value of time. time () has absolute meaning, and the other values have only relative meaning.

The get_clock_info function allows you to view the characteristics of these clocks, whose inputs and outputs are

implementation monotonic adjustable resolution
'time' GetSystemTimeAsFileTime() False True 0.015625
'thread_time' GetThreadTimes() True False 1e-07
'process_time' GetProcessTimes() True False 1e-07
'monotonic' GetTickCount64() True False 0.015625
'perf_counter' QueryPerformanceCounter() True False 1e-07

Among them,

adjustable is True if the clock can be changed automatically or manually by the system administrator, otherwise False. implementation represents the name of the underlying C function used to get the clock value. If the clock cannot go backwards, monotonic is True, otherwise it is False. resolution represents the clock resolution in seconds.

Next, you can test the characteristics of these clocks.


>>> def test(n):
...   aTime = time.time()
...   aTh = time.thread_time()
...   aPr = time.process_time()
...   aMo = time.monotonic()
...   aPe = time.perf_counter()
...   for i in range(int(n)): j = i**2
...   bTime = time.time()
...   bTh = time.thread_time()
...   bPr = time.process_time()
...   bMo = time.monotonic()
...   bPe = time.perf_counter()
...   aStr = f'aTime={aTime},aTh={aTh},aPr={aPr},aMo={aMo},aPe={aPe}\n'
...   bStr = f'bTime={bTime},bTh={bTh},bPr={bPr},bMo={bMo},bPe={bPe}'
...   print(aStr+bStr)
...
>>> test(1e6)
aTime=1634625786.136904,aTh=0.03125,aPr=0.03125,aMo=199082.078,aPe=199085.4751224
bTime=1634625786.340363,bTh=0.234375,bPr=0.234375,bMo=199082.281,bPe=199085.6787309
>>> test(1e6)
aTime=1634625789.7817287,aTh=0.234375,aPr=0.234375,aMo=199085.734,aPe=199089.1195357
bTime=1634625789.981198,bTh=0.421875,bPr=0.421875,bMo=199085.921,bPe=199089.3195721
>>> test(1e6)
aTime=1634625796.3934195,aTh=0.421875,aPr=0.421875,aMo=199092.343,aPe=199095.731209
bTime=1634625796.5789576,bTh=0.609375,bPr=0.609375,bMo=199092.531,bPe=199095.9172852
>>>

You can clearly see that when calling the test In the interval, thread_time And process_time Has not changed, that is, the two do not calculate the time when the thread or process sleeps.

1 in time Module, the two most commonly used functions are time.time() And time.sleep() The former is used to obtain the timestamp, so as to count the running time of the program; The latter can pause the thread.

It can be passed through time.thread_time() To detect sleep Function of function


>>> def test(n):
...    aTime = time.time()
...    aTh = time.thread_time()
...    aPr = time.process_time()
...    time.sleep(n)
...    bTime = time.time()
...    bTh = time.thread_time()
...    bPr = time.process_time()
...    aStr = f'aTime={aTime},aTh={aTh},aPr={aPr}\n'
...    bStr = f'bTime={bTime},bTh={bTh},bPr={bPr}'
...    print(aStr+bStr)
...
>>> test(1)
aTime=1634649370.2819958,aTh=0.640625,aPr=0.640625
bTime=1634649371.2862759,bTh=0.640625,bPr=0.640625
>>> test(1)
aTime=1634649372.72013,aTh=0.640625,aPr=0.640625
bTime=1634649373.723695,bTh=0.640625,bPr=0.640625
>>> test(1)

Time zone concept

Next, I need to introduce some concepts about time

GMT Greenwich Mean Time.

UTC Universal Coordinated Time, more accurate than Greenwich.

thread_time0 D is Daylight, which means daylight saving time.

CST Standard time in the United States, Australia, China and Cuba.

Once you know the concepts of these time zones, you can understand time Constants in:

常量 altzone daylight tzname timezone
时区偏移 如未定义非DST时区,则为0 时区名称 本地时区偏移

struct_time

In order to better express the time, time Encapsulated in the struct_time Class whose members include

索引 属性 含义
0 tm_year 正整数
1 tm_mon range [1, 12]
2 tm_mday range [1, 31] 月中的日期
3 tm_hour range [0, 23]
4 tm_min range [0, 59]
5 tm_sec range [0, 61]
6 tm_wday range [0, 6],周1为 0 星期即
7 tm_yday range [1, 366] 在1年中的第几天
8 tm_isdst 0, 1 或 -1 是否为DST
- tm_zone 时区名称的缩写
- tm_gmtoff 以秒为单位的UTC以东偏离

After understanding the 1 data structure struct_time, you can read the following functions.

单参函数
gmtime(secs) 将时间戳转化为UTC时间[struct_time格式]
localtime(secs) 将戳转化为本地时间[struct_time格式]
ctime(secs) 将时间戳转化为UTC时间字符串
asctime(secs) 将时间结构体转化为本地时间字符串
mktime localtime的反函数,将struct_time转为秒数

time.strftime(format[, t])

You can set the struct_time The output is formatted by matching characters, and its conversion format is

名称 含意 名称 含意
%a 星期的缩写 %A 星期的名称
%b 月份缩写 %B 月份名称
%c 适当的日期和时间表示
%d 月中日,范围[01,31] %j 年中日,范围[001,366]
%H 小时,范围[00,23] %I 小时,范围[01,12]
%M 分钟,范围[00,59] %S 秒,范围[00,61]
%p AM 或 PM
%m 月份,范围[01,12]
%U 年中周数,范围[00,53]
周日作为第1天
%W 同左,周1作为第1天
%w 周中日,范围[0(星期日),6]
%x 适当的日期表示 %X 适当的时间表示
%y 无世纪年份,范围[00,99] %Y 带世纪的年份
%z 时区偏移
%Z 时区名称
%% 字面的 ‘%' 字符。

strptime() Is its inverse function.

For example


>>> t = time.strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
>>> t
'Tue, 19 Oct 2021 13:46:37 +0000'
>>> T = time.strptime(t,"%a, %d %b %Y %H:%M:%S +0000")
>>> T
time.struct_time(tm_year=2021, tm_mon=10, tm_mday=19, tm_hour=13, tm_min=46, tm_sec=37, tm_wday=1, tm_yday=292, tm_isdst=-1)

The above is the detailed content that the table combs and analyzes the built-in time module of python. Please pay attention to other related articles on this site for more information about the built-in time module of python!


Related articles: