In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
I believe many inexperienced people don't know what to do about how to use the time library in Python. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Clocks comparison
The implementation of clock varies from platform to platform, and using getclockinfo can provide basic information about the current clock implementation, including the accuracy of clock.
Def clock_basic ():
Import textwrap
Available_clocks = [
('time', time.time)
('monotonic', time.monotonic)
('perf_counter', time.perf_counter)
('process_time', time.process_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 ())
)
) time
One of the core functions of the time module is time (), which returns the number of seconds after the start of epoch as a floating-point number. Opoch as the beginning of time measurement, for Unix systems, 0:00 on January 1, 1970, although this value is a floating-point number, the actual accuracy depends on the platform. Floating-point numbers are convenient for storing or comparing time, but are less readable, so the ctime () method is more useful for logging or printing time.
Def clock_time ():
Print (time.time ())
Print (time.ctime ()) monotone Clock
The user or system service modifies the system clock in order to synchronize the clock across machines, and the time () method looks at the system clock, so calling the time () method multiple times may produce forward or backward values. Unexpected behavior occurs when you use these values for calculations or when you try to measure continuous time. The monotonic () method avoids this situation because only the forward value is returned.
Def clock_monotonic ():
Start = time.monotonic ()
Time.sleep (0.1)
End = time.monotonic ()
Print (start)
Print (end)
The start time of the monotonic clock is defined, and the return value can only be used for calculation with other clock values.
Processor clock
Process_time () returns the processor clock time: represents the actual execution time of the processor, so the processor clock does not increase when the program is not doing anything.
Def processor_clock ():
Template ='{}-{: .2f}-{: .2f}'
Print (template.format (time.ctime (), time.time (), time.process_time ()
For i in range (3,0,-1):
Time.sleep (I)
Print (template.format (time.ctime (), time.time (), time.process_time () performance count
High-precision monotone clock is very important for performance measurement. The determination of the best clock data source depends on the relevant platform information, which is provided by Python in perf_counter.
Def perf_counter_clock ():
Loop_start = time.perf_counter ()
For i in range (5):
Iter_start = time.perf_counter ()
A = 1
For i in range (10000):
A = a + I
Now = time.perf_counter ()
Loop_elapsed = now-loop_start
Iter_elapsed = now-iter_start
Print ('{: .6f} {: .6f} '.format (loop_elapsed, iter_elapsed)) after reading the above, have you mastered how to use the time library in Python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.