A brief introduction to the Python time library

Basic introduction to the time library

expression of computer time

Provides the function of getting the system time and formatting the output

Provides system-level precise timing for program performance analysis

usage import time

time acquisition

function describe
time() Get the current timestamp, that is, the computer's internal time value, a floating-point number
such as: time.time() returns 154564641.1566853 is the second counted from 1970
ctime() Get the current time and represent it in a human-readable way, return a string
such as: time.ctime() "Fri Apr 13 23:59:37 2018"
gmtime () Get the current time, expressed as the time format currently processed by the computer,
such as: time.gmtime() The result is time.struct_time(tm_year=2018, tm_mon=4, tm_mday=13, tm_hour=16, tm_min=1, tm_sec=36, tm_wday=4, tm_yday=103, tm_isdst=0)
( Why is the time obtained by gmtime() not the current time )

time formatting

function describe
strftime.(tpl,ts) tpl is a formatted template string, used to define the output effect, ts is the computer's internal time type variable
>>>t = time.gmtime()
>>>time.strftime("%Y-%m-%d %H: %M%s:",t)
>>>'2018-04-13 16:08:18'
strptime(str,tpl) str is the time value in the form of a string
tpl is the string of the format template, which is used to define
the structure of the time returned by the input effect, which is the inverse of strftime()

Program timing application
function describe
perf_counter() Returns a CPU-level precise time count value in seconds, a floating-point number, and the difference between two times is the time in it
sleep(s) program waits s seconds

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325871832&siteId=291194637