关于python中timeit模块的问题

发布于 2024-11-28 07:04:56 字数 177 浏览 5 评论 0原文

我有一个关于python中timeit模块的问题,该模块用于确定一段代码执行所需的时间。

t = timeit.Timer("foo","from __main__ import foo")
str(t.timeit(1000))

上面代码中参数 1000 的含义是什么?

I have a question about the timeit module in python, which is used to determine the time it takes for a piece of code to execute.

t = timeit.Timer("foo","from __main__ import foo")
str(t.timeit(1000))

What is the meaning of the argument 1000 in the above code ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

记忆で 2024-12-05 07:04:56

来自文档

Timer.timeit([number=1000000])

主语句执行次数次的时间。这将执行
setup语句一次,然后返回执行所需的时间
主要语句的次数,以秒为单位作为浮点数测量。
参数是循环的次数,默认为
一百万。主语句、设置语句和定时器
要使用的函数被传递给构造函数。

From the documentation:

Timer.timeit([number=1000000])

Time number executions of the main statement. This executes the
setup statement once, and then returns the time it takes to execute
the main statement a number of times, measured in seconds as a float.
The argument is the number of times through the loop, defaulting to
one million. The main statement, the setup statement and the timer
function to be used are passed to the constructor.

淑女气质 2024-12-05 07:04:56

正如记录的,数字表示 timeit 应执行的次数指定的程序。

由于由于缓存,前几次执行可能会明显变慢,并且各个执行时间可能会有很大差异,因此更多的计时运行(即更高的值)将产生更精确的结果,但也会花费更长的时间。

As documented, the number indicates the number of times timeit should execute the specified program.

Since the first few executions can be significantly slower due to caching, and individual execution times may vary wildly, more timing runs (i.e. a higher value) will yield a more precise result, but also take longer.

我的奇迹 2024-12-05 07:04:56

本着授人以鱼的精神,问问Python:

>>> import timeit
>>> t=timeit.Timer()
>>> help(t.timeit)
Help on method timeit in module timeit:

timeit(self, number=1000000) method of timeit.Timer instance
    Time 'number' executions of the main statement.

    To be precise, this executes the setup statement once, and
    then returns the time it takes to execute the main statement
    a number of times, as a float measured in seconds.  The
    argument is the number of times through the loop, defaulting
    to one million.  The main statement, the setup statement and
    the timer function to be used are passed to the constructor.

In the spirit of teaching a man to fish, just ask Python:

>>> import timeit
>>> t=timeit.Timer()
>>> help(t.timeit)
Help on method timeit in module timeit:

timeit(self, number=1000000) method of timeit.Timer instance
    Time 'number' executions of the main statement.

    To be precise, this executes the setup statement once, and
    then returns the time it takes to execute the main statement
    a number of times, as a float measured in seconds.  The
    argument is the number of times through the loop, defaulting
    to one million.  The main statement, the setup statement and
    the timer function to be used are passed to the constructor.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文