我想测量以下代码的执行速度:
def pe1():
l = []
for i in range(1000):
if i%3 == 0 or i%5 == 0:
l.append(i)
print sum(l)
Run Code Online (Sandbox Code Playgroud)
我将此代码存储在pe1m.py下.现在我想用python解释器测试文件的速度.我做了:
import timeit
import pe1m
t = timeit.Timer(stmt = 'pe1m.pe1()')
t.timeit()
Run Code Online (Sandbox Code Playgroud)
但我得到:
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/timeit.py", line 195, in timeit
timing = self.inner(it, self.timer)
File "<timeit-src>", line 6, in inner
NameError: global name 'pe1m' is not defined
Run Code Online (Sandbox Code Playgroud)
但我没有任何全局变量.
我不明白之间的差别number,并repeat在timeit库中,所以会请你告诉我它们之间有什么区别呢?
我正在尝试使用timeit模块,但我不知道如何.我有一个主要的:
from Foo import Foo
if __name__ == '__main__':
...
foo = Foo(arg1, arg2)
t = Timer("foo.runAlgorithm()")
print t.timeit(2)
Run Code Online (Sandbox Code Playgroud)
我的Class Foo有一个名为runAlgorithm()的方法
错误是这样的:
NameError:未定义全局名称'foo'
我究竟做错了什么?我可以从课堂方法中抽出时间吗?
我有一个用于代码优化的timit函数的问题.例如,我在文件中编写带参数的函数,让我们调用它myfunctions.py包含:
def func1(X):
Y = X+1
return Y
Run Code Online (Sandbox Code Playgroud)
我在第二个文件test.py中测试这个函数,我调用计时器函数来测试代码性能(显然更复杂的问题!)包含:
import myfunctions
X0 = 1
t = Timer("Y0 = myfunctions.func1(X0)")
print Y0
print t.timeit()
Run Code Online (Sandbox Code Playgroud)
在Y0不计算,即使我评论print Y0线错误global name 'myfunctions' is not defined发生.
如果我使用命令指定安装程序
t = Timer("Y0 = myfunctions.func1(X0)","import myfunctions")
Run Code Online (Sandbox Code Playgroud)
现在global name 'X0' is not defined发生了错误.
有人知道如何解决这个问题吗?非常感谢.
我认为这三个在逻辑上是等价的,返回集合{1, 3, 4}:
set(sum(((1, 3), (4,), (1,)), ()))
set(sum([[1, 3], [4], [1]], []))
functools.reduce(operator.or_, ({1, 3}, {4}, {1}), set())
Run Code Online (Sandbox Code Playgroud)
但是当我尝试在ipython(python 3.4.0上的v1.2.1)中检查每个的性能时,timeit magic失败了.
In [1]: from operator import or_; from functools import reduce
In [2]: timeit set(sum([[1, 3], [4], [1]], []))
1000000 loops, best of 3: 604 ns per loop
In [3]: timeit set(sum(((1, 3), (4,), (1,)), ()))
1000000 loops, best of 3: 330 ns per loop
In [4]: timeit reduce(or_, ({1, 3}, {4}, {1}), set())
---------------------------------------------------------------------------
TypeError Traceback …Run Code Online (Sandbox Code Playgroud) 尝试对不同的随机函数进行计时,以查看从列表中选择随机项目的最快方法。%timeit想要给我“3个中最好的”最快时间,但由于运行是随机的,因此访问时间存在很大差异(从列表后面抓取,会很慢;从前面抓取,会很快)。
如何获得所有循环的平均值,而不是最好的?
\n\na = [0,6,3,1,3,9,4,3,2,6]\n\n%timeit random.choice(a)\n%timeit a[random.randint(0,len(a)-1)]\n%timeit a[np.random.randint(0,len(a)-1)]\n%timeit np.random.choice(a,1)[0]\nRun Code Online (Sandbox Code Playgroud)\n\n当前输出(确认时间差异):
\n\n%timeit random.choice(a)\nThe slowest run took 9.87 times longer than the fastest. This could mean that an intermediate result is being cached \n1000000 loops, best of 3: 1.23 \xc2\xb5s per loop\nRun Code Online (Sandbox Code Playgroud)\n\n更新:一种拼凑方法:
\n\n%time for i in range(100000): random.choice(a)\n%time for i in range(100000): a[random.randint(0,len(a)-1)]\n%time for i in range(100000): a[np.random.randint(0,len(a)-1)]\n%time for i in range(100000): np.random.choice(a,1)[0]\nRun Code Online (Sandbox Code Playgroud)\n 我正在尝试运行以下代码,但我得到了local variable 'a' referenced before assignment.
a = [x for x in range(10)]
b = [x for x in range(10)]
%timeit a+=b
Run Code Online (Sandbox Code Playgroud)
该声明无需%timeit魔法即可发挥作用。
我有什么遗漏的吗?
谢谢。
我想优化一个函数“ myfunc() ”。我有几种写法,我想检查一下最快的代码。为此,我们可以使用“ timeit ”模块。但是有几种方法可以使用它。最明显,显然也是最常用的,是:
import timeit
timeit.Timer('myfunc()', "from __main__ import myfunc").timeit(100000)
Run Code Online (Sandbox Code Playgroud)
这类似于
timeit.timeit('myfunc()', "from __main__ import myfunc", number=100000)
Run Code Online (Sandbox Code Playgroud)
但是我们也可以使用这样的代码:
min(timeit.Timer('myfunc()', "from __main__ import myfunc").repeat(repeat=100000, number=1))*100000
Run Code Online (Sandbox Code Playgroud)
我期待最后一个是最准确的处理时间,但它似乎不是最常用的代码。
你能帮我确定什么时候使用timeit.timeit更好,什么时候重复更合适吗?
提前致谢
我想比较两个片段的执行时间,看看哪一个更快。因此,我想要一种准确的方法来测量 python 代码片段的执行时间。
我已经尝试过使用time.time()、time.process_time()、time.perf_counter_ns()以及timeit.timeit(),但我面临着与所有这些相同的问题。也就是说:当我使用上述任何方法来测量同一代码片段的执行时间时,每次运行它时它都会返回不同的值。这种变化有些显着,以至于我无法可靠地使用它们来比较两个片段的执行时间差异。
例如,我在我的 google colab 中运行以下代码:
import time
t1 = time.perf_counter()
sample_list = []
for i in range(1000000):
sample_list.append(i)
t2 = time.perf_counter()
print(t2 - t1)
Run Code Online (Sandbox Code Playgroud)
我运行上面的代码 10 次,结果的变化约为 50%(最小值 = 0.14,最大值 = 0.28)。
还有其他选择吗?