Scripting Language Learning (four)

Use time library

  • time library is a standard library python in processing time
  • Use import timeand time.<b>()call the formats you can use

Action : Expression of computer time; providing a system time and output formatting functions; providing system-level precision timing functions, performance analysis can be used to

time library contains three types of functions:

  • Time acquisition: time (), ctime (), gmtime ()
  • Time Format: strftime (), strptime ()
  • Timing the application: sleep (), perf_counter ()

Time to get

  • Timestamp: refers to the current system time stamp representing a floating-point time, is the time value of the internal computer
  • time () function: Get the current timestamp, returns a string of long float
  • the ctime () Function: Get the current time and is readable manner, said returns a string. E.gSun Feb 9 13:50:13 2020
  • gmtime, () Function: Get the current time, the time expressed as machine-processable format (struct_time format)

Time Format

  • Time format is the time to show up at a reasonable ways
  • Similar to the string formatting (.format), need to show template
  • Display format template by the particular control specifier

strftime function

strftime function takes two parameters

  • tpl parameter string format template that defines the output effect
  • ts is the computer's internal Time Tag Type

Examples :

>>> t=time.gmtime()
>>> time.strftime("%Y-%m-%d %H:%M:%S",t)

Can output'2020-02-09 08:30:48'

Format string


strptime function

  • strptime strftime function and are complementary
  • Can be a string into a computer's internal time can be operated
  • Str string parameter is a time value, TPL formatted template string, to define the effect of input

For example :

>>> timestr="2019-02-09 17:03:20"
>>> time.strptime(timestr,"%Y-%m-%d %H:%M:%S")

结果为:time.struct_time(tm_year=2019, tm_mon=2, tm_mday=9, tm_hour=17, tm_min=3, tm_sec=20, tm_wday=5, tm_yday=40, tm_isdst=-1)

Program timing applications

  • Timing the application means during operation of the measurement start and end time experienced by
  • For example a program from the beginning to the end run gone much time, called the program timing
  • Timing the application including measurement time and generation time of two parts
  • Time measuring means is capable of recording the passage of time

Measurement time: perf_counter ()

  • You can get its CPU clock frequency operation, high accuracy

Generation time: sleep ()

  • Can let the program sleep or have a certain time

Guess you like

Origin www.cnblogs.com/CCchaos/p/12287835.html