In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
2025-11-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how Python uses the timeit module. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
timeit module
The timeit module provides a way to measure the execution time of small Python code pieces, either directly from the command-line interface or via an import module.
Number of times the statement is executed
#import timeit.timeitfrom timeit import timeit#see the execution time of x=1 (number can be omitted, default is 100000)t1 = timeit ('x= 1', number=1)print(t1)#Execution time of a list generator, executed 10000 times: t2 = timeit ('[i for i in range(100) if i%2==0 $>', number=10000)print(t2) Output: 4.00000001150227e-070.04841430000002
Compute the execution time of a function
from timeit import timeitdef func(a): sum = 0 for i in range(a): sum += i return sum# timeit(function name_string, runtime environment_string, number= number of runs)t = timeit ('func (1000000)', 'from __main__ import func', number=1)print(t) Output: 0.4887406 Repeatedly calling timeit()
Repeat() is equivalent to calling timeit() several times over and returning the result as a list. The repeat parameter specifies the number of repetitions, and the number parameter is passed to the number parameter of the timeit() method.
import timeitdef func(a): sum = 0 for i in range(a): sum += i return sum# timeit(function name_string, running environment_string, number= run times,repeat= repeat times, repeat=5)t = timeit.repeat ('func (100000)', 'from __main__ import func', number=100, repeat=5)print(t) Thank you for reading! About "python how to use timeit module" This article is shared here, I hope the above content can have some help for everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!
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.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.