简体   繁体   English

Generators和Timeit ......我读错了结果吗?

[英]Generators and Timeit… am I reading the results wrong?

I'm using Python 2.7.10. 我正在使用Python 2.7.10。

I was going through a tutorial on generators, and understand that xrange , when called, returns a generator, so I am expecting it to be faster when calculating a sum. 我正在阅读有关生成器的教程,并了解xrange在被调用时会返回一个生成器,因此我希望它在计算总和时更快。

I am confused about the whole 1 loop vs 10 loops and what 1 loop, best of 3 means considering there was only 1 loop? 我对整个1循环对10循环和1 loop, best of 3感到困惑1 loop, best of 3意味着只有1循环?

How does timeit know how many loops to do, or is that a function of the code I'm applying timeit to? timeit如何知道要执行多少循环,或者是我正在应用timeit的代码的函数?

I do not know how to interpret the results here.... Advice? 我不知道如何解释这里的结果....建议?

时间比较

The time given by %timeit magic command of IPython is the time taken for the statement. 由IPython的%timeit magic命令给出的时间是该语句所花费的时间。

In case of xrange it only took 68.2 milliseconds, whereas range took 243 milliseconds , hence xrange really is faster than range as you are expecting. 在的情况下, xrange只花了68.2毫秒,而range了243毫秒,所以xrange确实比快range ,你期待。

The result printing might seem a little weird unless you read the docs carefully. 除非您仔细阅读文档,否则结果打印可能看起来有点奇怪。

The basic operation of timeit (or %timeit which uses the former) is: timeit的基本操作(或使用前者的%timeit)是:

\-- repeat timer loop n times (return best of t_i / m)
  \-- timed run (time t_i is measured)
    \-- statement loop
          (unless specified, m = 1, 10, 100, ..., until t_i > 0.2s
            t_i = sum(t_s)
          )
      \-- statement (t_s, time per statement)

What is displayed is max(t_i) . 显示的是max(t_i)

  1. First number ( n loops ) is the number of loops per timer run. 第一个数字( n loops )是每个计时器运行的循环数。
  2. Second number ( best of m ) is the number of timer runs. 第二个数字( best of m )是计时器运行的数量。
  3. Third number is the time per statement execution of the best run from (2). 第三个数字是从(2)开始执行最佳运行的每个语句的时间。

By default timeit runs the statement once for each timed loop and it runs three timed loops. 默认情况下, timeit为每个定时循环运行一次语句,并运行三个定时循环。 The corresponding switch to change the number of timed loops is -r . 用于更改定时循环数的相应开关是-r

However, if the running time of a timer loop is less than 0.2 seconds, it repeats the timed statement inside a timer loop. 但是,如果定时器循环的运行时间小于0.2秒,它会在定时器循环内重复定时语句。 First it goes up to 10 times, then 100 times, and so on. 首先它上升到10次,然后是100次,依此类推。 This is the first number. 这是第一个数字。 The corresponding switch to fix this number is -n . 修复此编号的相应开关是-n

So range() ran once for a single timer loop, because it took more than 0.2 seconds. 因此range()为单个定时器循环运行一次,因为它花了超过0.2秒。 A single xrange() sum, however, took less 0.2 seconds, so it was repeated 10 times, which was enough to push it over the boundary (ten runs took 0.682 seconds). 然而,单个xrange()总和花费的时间少于0.2秒,因此它重复了10次,这足以将其推到边界上(十次运行需要0.682秒)。

The resulting running time still is the running time of a single statement. 由此产生的运行时间仍然是单个语句的运行时间。 The rest is typical benchmark smoothing stuff. 其余的是典型的基准平滑材料。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM