Python day 6 time library
- The most basic processing time of the standard library,
(1) may be used for the expression of computer time
(2) the time acquisition system, formatted output time
(3) precise timing functions, can be used for performance analysis
- Three Kinds of Functions
(1) Time Acquisition: Time () ctime () gmtime ()
(2) Time Format: the strftime () the strptime ()
(3) program timer sleep () perf_counter ()
- Time to get
(1) are no parameters
(2) Time () to get the current timestamp is a long float,
1573730037.1486392
(3) Ctime () to get an easy time understanding of the string " week - month - date - when minutes and seconds - Year "
'Thu Nov 14 19:15:18 2019'
(4) Gmtime () generates a time format can be processed by a computer
time.struct_time(tm_year=2019, tm_mon=11, tm_mday=14, tm_hour=11, tm_min=17, tm_sec=6, tm_wday=3, tm_yday=318, tm_isdst=0)
- Time Output Formatting
(1) Similarly formatted output string, showing the need for a template, the template display format control characters by a specific composition
(2) Strftime (tpl, ts ) Method
tpl string, time below mentioned control character
ts is calculated and the internal time type variable
(3) Strptime (str, tpl ) Method
Str is a string time
Tpl time control character
Import time as t
a = t.gmtime()
>>> t.strftime('%Y-%m-%d %H:%M:%S',a)
'2019-11-14 11:38:58'
(4) time control character
①% Y Year
②% m month number
③% B name of the month in English
④% b month abbreviation abbreviation
⑤% A week English
⑥% a week abbreviation abbreviation
⑦% H hour 24-hour clock
⑧% I hour 12-hour clock
⑨ %p AM/PM
⑩% M min digital
⑪% S second digital
- Time Measurement
(1) Precision measurement time perf_counter () in nanoseconds
Similar time stamp, to measure the time difference is calculated
start = t.perf_counter()
>>> end = t.perf_counter()
>>> end-start
14.1491517999998
At the moment the assignment of function to complete his mission
(2) Timing the application widely used
(3) Sleep (s) so that sleeps s seconds
import time as t
def wait():
t.sleep(3.3)
wait()
print('sjdk')
- Example: Text the progress bar
(1) Requirements: The character string printed text can be dynamically changing the progress of the progress bar needs to be able progressively converted in a row
(2) simple start
import time
scale = 10
print ( '------- begin ---------')
for i in range(scale+1):
a = '*'*i
b = '.'*(scale-i)
c = (i/scale)*100
print('{:^3.0f}%[{}->{}]'.format(c,a,b))
time.sleep(0.1)
print ( '{: - ^ 20 }'. format ( ' end of execution '))
------- started ---------
0 %[->..........]
10 %[*->.........]
20 %[**->........]
30 %[***->.......]
40 %[****->......]
50 %[*****->.....]
60 %[******->....]
70 %[*******->...]
80 %[********->..]
90 %[*********->.]
100%[**********->]
-------- execution ends --------
(3) single-line dynamic refresh
① refreshing nature: before printed with the character after character covered
② can not change the line, but also to make the cursor to return to the original position
③ import time as t
④ for i in range(101):
⑤ print('\r{:3}%'.format(i),end='')
⑥ t.sleep(0.2)
IDLE shields \ r function
In the cmd run incremental refresh schedule percentage in effect
(4) full effect
import time
scale = 10
print ( '------- begin ---------')
for i in range(scale+1):
a = '*'*i
b = '.'*(scale-i)
c = (i/scale)*100
print('{:^3.0f}%[{}->{}]'.format(c,a,b))
time.sleep(0.1)
print ( '{: - ^ 20 }'. format ( ' end of execution '))