Chapter IV Python 3 standard library

Chapter IV
Date and Time
   Unlike int, float and str, Python is not native type comprising a corresponding date and time, but offers three respective modules may be employed to manage multiple representations of a date and time value.
   time module provides functions related to time by the underlying C library. It contains functions that can be used to obtain the clock time processor and run time, but also provides a basic string parsing and formatting tools.
   datetime module provides a higher level interface to the date, time, and date and time. datetime in class supports arithmetic, comparison, and time zone configuration.
   calendar module can create a weekly, monthly and annual formatted representation. It can also be used to calculate the recurring event, given the date a few, as well as other value-based calendar week.
  
4.1 time: time clock
 
 time module allows access to a plurality of types of clocks respectively used for different purposes. Standard system calls (such as time ()) will be reporting system "wall clock" time. monitonic () clock can be used to measure a
process of long-running elapsed time (elapsed time), because even if there is time to change the system, but also to ensure that the clock will not be reversed. For performance testing, perf_counter () allows access to the highest available
resolution of the clock, which makes the measurement more accurate short. CPU time () can be obtained by the clock, process_time () returns the combined results of the processor and system time.
-------------------------------------------------- -------------------------------------------------- -------------------
Description: these implementations provide some C library functions for managing the dates and times. Since they are bound to the underlying C implementation details (such as the maximum value of the epoch start date and time support) will be specific to a particular platform.
To learn more about the comprehensive. Refer to the documentation.
-------------------------------------------------- -------------------------------------------------- -------------------
4.1.1 comparison clock
--------------------
 implementation details of the clock due to platform It varies. You may be used get_clock_info () to obtain basic information currently implemented, including the resolution of the clock.
 
     Listing 4-1: time_get_clock_info.py
     ------------------------------------
     Import TextWrap
     Import Time
     
     available_clocks = [
      ( 'Clock', time.clock),
      ( 'Monotonic', time.monotonic),
      ( 'perf_counter', time.perf_counter),
      ( 'process_time', time.process_time),
      ( 'Time', the time.time ),
     ]
     
     for clock_name, FUNC in available_clocks:
      Print (textwrap.dedent ( '' '\
      {name}:
       Adjustable: {} info.adjustable
       Implementation: {} info.implementation
       Monotonic: {} info.monotonic
       Resolution: {} info.resolution
       Current: Current {}
       ' '' ) .format (
       name = clock_name,
       info = time.get_clock_info (clock_name),
       Current = FUNC ())
       )
     -------------------------- ------------
 following output display on Mac OS X, monotonic and perf_counter clock is accomplished by calling the same underlying system.
------------------
4.1.2 on the wall clock time
------------------
 one of the core function of time module is the time (), he will start from the number of seconds since the "epoch" returned as a floating point value.
------------------
 era is the starting point of time measurement, for UNIX systems, the start time is at 00:00 on January 1, 1970. Although this value is always a floating point number, but the specific accuracy depends on the specific platform.
------------------
 floating point representation for limited storage or comparison-day period, but for the generation of human-readable representation of a bit far from satisfactory. To record or print time, ctime () may be a better choice.
     Listing 4-3: time_ctime.py
     ---------------------------
     Import Time
     Print ( 'Time of The IS:', time.ctime ())
     later the time.time = () + 15
     Print (secs from now '15: ', time.ctime (later))
     --------- ------------------
 this example, the second print () call to show how to use the ctime () other non-formatted time value of the current time.
------------------
4.1.3 monotonic clock
------------------
 due time () Check the system clock, and service user or the system may change the system clock to synchronize the clocks on the plurality of computers, so called repeatedly time () value may be generated by forward and backward.
When you try to use them to measure the duration or time to complete the calculation, which may lead to unexpected behavior. To avoid these situations, use monotonic (). It always returns the value forward.
------------------
     Listing 4-4: time_monotonic.py
     ----------------------- -------
     Import Time
     time.monotonic = Start ()
     the time.sleep (0.1)
     End = time.monotonic ()
     Print ( 'Start: {:> 9.2f}'. the format (Start))
     Print ( 'End: {:> 9.2f}' .format (End))
     Print ( 'span: {:> 9.2f}'. the format (End - Start))
     ------------------------ -------
 starting monotonic clock is not defined, the return value is only useful when the other clock values to complete the calculation. In this example, using a monotonic () to measure sleep duration.
--------------------
4.1.4 processor clock time
--------------------
 Time () returns a wall clock time, the clock () returns the processor clock time. clock () return value reflects the actual use of the time the program is run.
--------------------
     Listing 4-5: time_clock.py
     --------------------- -------
     Import hashlib
     Import Time
     To use the Data to the Calculate # checksums
     Data = Open ( 'D: \ Zhejiang banks \ IP.xlsx', 'RB') Read ().
     
     For I in Range (. 5):
      H = hashlib.sha1 ()
      Print (Time .ctime (), ': {:} {0.3f: 0.3f}' the format (the time.time (), time.clock ())).
      for I in Range (300000):
       h.update (Data)
      the cksum = h.digest ()
     ----------------------------
 in this example, each loop iteration, the formatted print ctime () time, and time () and clock () returns a floating-point value.
 --------------------------------------------
 Note: If you want to run this example on your system, you may have to add more inner loop cycle, or to handle larger amounts of data, this can really see the difference in time.
 --------------------------------------------
 Under normal circumstances, if the program nothing has been done, the processor clock does not "tick" (tick).
 --------------------------------------------
 
     Listing 4-6: time_clock_sleep.py
     ----------------------------------
     Import Time
     = Template '{} - {: 0.2f} - {:} 0.2f'
     
     Print (template.format (
      time.ctime (), the time.time (), time.clock ())
     )
     
     for I in Range (. 3, 0, -1):
      Print ( 'Sleeping', I)
      the time.sleep (I)
      Print (template.format (
       time.ctime (), the time.time (), time.clock ())
       )
     ----- ----------------------------
 in this example, the cycle is almost not do any work, after each iteration of sleep. The application of sleep, time () value will increase, while the clock () value does not increase.
 ----------------------------
4.1.5 Performance counters
 during the test performance, high-resolution clock is essential. To determine the best clock data sources, you need to have specific knowledge of the platform, Python by perf_counter () to provide the required knowledge.
    
     Listing 4-7: time_perf_counter.py
     ---------------------------------
     Import hashlib
     Time Import
     
     # to use the Data MD5 checksums to the Calculate
     Data = Open ( 'D: \ Zhejiang banks \ IP.xlsx', 'RB') Read ().
     
     loop_start time.perf_counter = ()
     
     for I in Range (. 5):
      = time.perf_counter iter_start ()
      H = hashlib.sha1 ()
      for I in Range (300000):
       h.update (Data)
      the cksum = h.digest ()
      now time.perf_counter = ()
      loop_elapsed = now - loop_start
      iter_elapsed now = - iter_start
      Print (time.ctime (),. ': {:} {0.3f: 0.3f}' the format (iter_elapsed, loop_elapsed))
     ------------------- ----------------
 similar monitonic (). perf_counter () defined epoch, so the return value and calculated value used for comparison only and not as an absolute time.
 ---------------------------
4.1.6 Time composition
 in some cases need to be stored as time passed the number of seconds (in seconds), but in other cases, the date of a program needs to access each field (eg, month and year). struct_time module defines the time
 to hold date and time values, wherein the decomposition of the component parts for easy access. Many functions have to deal with struct_time values instead of floating-point values.
 ---------------------------
4.1.7 processing zone
------------------ -----------------

Guess you like

Origin www.cnblogs.com/niaocaizhou/p/11759318.html