为什么if True比if 1Python 慢?不if True应该比快if 1?
我试图学习这个timeit模块.从基础知识开始,我尝试了以下方法:
>>> def test1():
... if True:
... return 1
... else:
... return 0
>>> print timeit("test1()", setup = "from __main__ import test1")
0.193144083023
>>> def test2():
... if 1:
... return 1
... else:
... return 0
>>> print timeit("test2()", setup = "from __main__ import test2")
0.162086009979
>>> def test3():
... if True:
... return True
... else:
... return False
>>> …Run Code Online (Sandbox Code Playgroud) 我希望捕获并绘制5个左右timeit调用的结果,并以对数增加N的大小来显示methodX()带有输入的比例.
到目前为止,我尝试过:
output = %timeit -r 10 results = methodX(N)
Run Code Online (Sandbox Code Playgroud)
这是行不通的...
无法在文档中找到信息.我觉得你应该能够至少拦截打印的字符串.之后,我可以解析它以提取我的信息.
有没有人这样做或尝试过?
PS:这是一个ipython笔记本,如果这是一个差异.
我在for循环中使用sklearn运行几个机器学习算法,并希望看到每个机器学习算法需要多长时间.问题是我还需要返回一个值,并且DONT想要多次运行它,因为每个算法都需要很长时间.有没有办法使用python的timeit模块或类似的函数捕获返回值'clf'...
def RandomForest(train_input, train_output):
clf = ensemble.RandomForestClassifier(n_estimators=10)
clf.fit(train_input, train_output)
return clf
Run Code Online (Sandbox Code Playgroud)
当我这样调用函数时
t = Timer(lambda : RandomForest(trainX,trainy))
print t.timeit(number=1)
Run Code Online (Sandbox Code Playgroud)
PS我也不想设置全局'clf',因为我可能想稍后进行多线程或多处理.
我遇到了一个我无法解释的奇怪情况.这是我的测试时间生成一大堆元组:
In [1]: def get_list_of_tuples():
...: return [(i,) for i in range(10**6)]
...:
In [2]: %time res = get_list_of_tuples()
CPU times: user 0.93 s, sys: 0.08 s, total: 1.01 s
Wall time: 0.98 s
In [3]: %timeit res = get_list_of_tuples()
1 loops, best of 3: 92.1 ms per loop
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,这个大型元组列表的生成只需不到一秒钟.timeit报告执行时间约为0.1秒.为什么这两份报告有这么大的差异?
(在IPython 0.11,Python 2.6.5上测试过.)
我不知道如何解释Python的timeit.timeit()函数的输出.我的代码如下:
import timeit
setup = """
import pydash
list_of_objs = [
{},
{'a': 1, 'b': 2, 0: 0},
{'a': 1, 'c': 1, 'p': lambda x: x}
]
"""
print(timeit.timeit("pydash.filter_(list_of_objs, {'a': 1})", setup=setup))
Run Code Online (Sandbox Code Playgroud)
这个输出是11.85382745500101.我该如何解释这个数字?
我注意到如果我遍历我打开的文件,迭代它而不"读取"它会快得多.
即
l = open('file','r')
for line in l:
pass (or code)
Run Code Online (Sandbox Code Playgroud)
比...快得多
l = open('file','r')
for line in l.read() / l.readlines():
pass (or code)
Run Code Online (Sandbox Code Playgroud)
第二个循环将花费大约1.5倍的时间(我在完全相同的文件上使用timeit,结果是0.442对0.660),并且会得到相同的结果.
那么 - 我什么时候应该使用.read()或.readlines()?
因为我总是需要迭代我正在阅读的文件,并且在经过艰难的学习之后,.read()对大数据的缓慢感觉 - 我似乎无法想象再次使用它.
我试图比较两个语句的性能,timeit结果如下:
100 loops, best of 3: 100 ns per loop
100 loops, best of 3: 1.96 us per loop
Run Code Online (Sandbox Code Playgroud)
但我不知道这些ns和us代表什么,所以我不知道哪一个更快.
当我在timeit()之外运行下面的代码时,它似乎立即完成.但是当我在timeit()函数中运行它时,需要更长的时间.为什么?
>>> import timeit
>>> t = timeit.Timer("3**4**5")
>>> t.timeit()
16.55522028637718
Run Code Online (Sandbox Code Playgroud)
使用:Python 3.1(x86) - AMD Athlon 64 X2 - WinXP(32位)
我用timeit获得了非常令人惊讶的结果,有人可以告诉我,如果我做错了吗?我使用的是Python 2.7.
这是文件speedtest_init.py的内容:
import random
to_count = [random.randint(0, 100) for r in range(60)]
Run Code Online (Sandbox Code Playgroud)
这些是speedtest.py的内容:
__author__ = 'BlueTrin'
import timeit
def test_init1():
print(timeit.timeit('import speedtest_init'))
def test_counter1():
s = """\
d = defaultdict(int);
for i in speedtest_init.to_count:
d[i] += 1
"""
print(timeit.timeit(s, 'from collections import defaultdict; import speedtest_init;'))
def test_counter2():
print(timeit.timeit('d = Counter(speedtest_init.to_count);', 'from collections import Counter; import speedtest_init;'))
if __name__ == "__main__":
test_init1()
test_counter1()
test_counter2()
Run Code Online (Sandbox Code Playgroud)
控制台输出是:
C:\Python27\python.exe C:/Dev/codility/chlorum2014/speedtest.py
2.71501962931
65.7090444503
91.2953839048
Process finished with exit code 0
Run Code Online (Sandbox Code Playgroud)
我认为默认情况下timeit()运行代码的1000000倍,所以我需要将时间除以1000000,但令人惊讶的是Counter慢于defaultdict().
这是预期的吗?
编辑:
使用dict也比defaultdict(int)更快: …
有没有办法使用timeit函数同时输出函数结果和处理时间?
现在我正在使用
timer = Timer('func()', 'from __main__ import func')
print timer.timeit(1)
Run Code Online (Sandbox Code Playgroud)
但这只是输出时间而不是程序输出,它会在结束时返回一些内容.我希望它输出
FuncOutputGoesHere 13.2897528935
Run Code Online (Sandbox Code Playgroud)
在同一条线上.
理想情况下,我希望能够通过运行N次来获取程序的平均值,然后输出程序结果及其平均时间(总共一个输出)
timeit ×10
python ×9
ipython ×2
python-2.7 ×2
boolean ×1
counter ×1
defaultdict ×1
if-statement ×1
io ×1
performance ×1
python-3.4 ×1
python-3.x ×1
scikit-learn ×1
time ×1
timer ×1