- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
IPython timeit
在使用设置文字时有时会被破坏:
In [1]: timeit 'potato' in {'spam', 'eggs', 'potato'}
10000000 loops, best of 3: 27.6 ns per loop
In [2]: timeit 'potato' in {'potato'}
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-2-9f61653b85de> in <module>()
----> 1 get_ipython().magic(u"timeit 'potato' in {'potato'}")
/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.pyc in magic(self, arg_s)
2203 magic_name, _, magic_arg_s = arg_s.partition(' ')
2204 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2205 return self.run_line_magic(magic_name, magic_arg_s)
2206
2207 #-------------------------------------------------------------------------
/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.pyc in run_line_magic(self, magic_name, line)
2124 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
2125 with self.builtin_trap:
-> 2126 result = fn(*args,**kwargs)
2127 return result
2128
/usr/local/lib/python2.7/dist-packages/IPython/core/magics/execution.pyc in timeit(self, line, cell)
/usr/local/lib/python2.7/dist-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k)
191 # but it's overkill for just that one bit of state.
192 def magic_deco(arg):
--> 193 call = lambda f, *a, **k: f(*a, **k)
194
195 if callable(arg):
/usr/local/lib/python2.7/dist-packages/IPython/core/magics/execution.pyc in timeit(self, line, cell)
1011 number = 1
1012 for _ in range(1, 10):
-> 1013 if timer.timeit(number) >= 0.2:
1014 break
1015 number *= 10
/usr/lib/python2.7/timeit.pyc in timeit(self, number)
193 gc.disable()
194 try:
--> 195 timing = self.inner(it, self.timer)
196 finally:
197 if gcold:
<magic-timeit> in inner(_it, _timer)
NameError: global name 'potato' is not defined
IPython v2.2.0,在 python 2.x 和 python3 上均失败。
最佳答案
这是一个bug in IPython ,由 ipython 魔法中扩展 {var} 引用的语法与 python 开发人员为集合文字选择的相同语法冲突引起。
如果遇到此问题,可能的解决方法是使用双花括号转义集合文字:
In [1]: timeit 0 in {0}
# TypeError: argument of type 'int' is not iterable
In [2]: timeit 0 in {{0}}
10000000 loops, best of 3: 60.1 ns per loop
关于python - 为什么 IPython 的 timeit 不能与集合文字一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25992510/
我正在使用 timeit.timeit 函数来计时我自己的函数之一,但返回的时间是科学计数法。 import timeit def func(): ... print(timeit.timei
我正在尝试编写一个简单的时间装饰器来测量函数所花费的时间。但是下面的代码给出了我们的递归错误。有什么问题吗? import timeit def measure(func): def wrap
import timeit t=timeit.timeit('x=map(lambda x:x*10,range(32))') print(t) 这表现了: 0.4678139 但 t=timeit.
我一直在尝试执行一个代码片段来了解执行需要多长时间。我尝试了两种选择来做到这一点。一种是在 timeit.timeit 函数内使用变量并进行检查。另一种是直接使用值并检查。第二种方法工作正常,但我在使
假设我有一些函数接受一个数组并将每个元素更改为 0。 def function(array): for i in range(0,len(array)): array[i] =
有没有办法在 python 中为 timeit.timeit() 函数指定拆卸?我知道有关于“声明”和“设置”的争论,但我还没有找到执行拆卸的好方法。 在我的“声明”中包含拆解代码会影响我尝试计时的代
有时,我喜欢计算我的部分代码运行需要多长时间。我检查了很多在线网站,并且看到了两种主要的方法来做到这一点。一种是使用time.time,另一种是使用timeit.timeit。 所以,我写了一个非常简
我想从 python timeit 模块检查打印以下内容需要多少时间,如何打印, import timeit x = [x for x in range(10000)] timeit.timeit("
我在 Project Euler 上工作过问题并决定添加计时,因此通过以下代码段将 timeit 添加到时间 main()(将全局 RESULT 存储在 main( ) 为了方便起见) t = tim
当我执行 help('timeit') 时,位于 timeit 模块(Python(2.7) 标准库)中的以下函数不会显示。 def reindent(src, indent): """Hel
考虑这段 Python3 代码: def classic_fibonacci(limit: int) -> List[int]: nums = [] curr, nxt = 0, 1
令人惊讶的 ipython 魔法 %timeit 错误: In[1]: a = 2 In[2]: %timeit a = 2 * a Traceback (most recent call last)
我有一个函数 f(x),它将 0 和 1 之间的 100 个随机 float 的列表 x 作为输入。不同的列表将导致不同的运行时间f. 我想知道 f 在大量不同的随机列表上平均运行多长时间。最好的方法
我想检查在合并 2 个数据帧时使用数字查找值是否比使用字符串查找值更快。为此,我将 %timeit 与以下代码一起使用: 合并字符串查找值: %timeit newframe = subframe.m
我正在计时从 python set 和 list 中删除元素。我的列表 timeit 代码引发了 ValueError: ... x not in list,但仅当我使用 timeit 运行不止一次迭
我想以便宜的方式测量函数的执行时间,如下所示: def my_timeit(func, *args, **kwargs): t0 = time.time() result = func
我想在 Python 3.5 中使用 timeit 来测量两个函数。第一个依赖于 import math,第二个依赖于 from math import log2。虽然我可以通过在调用 timeit.
更新:显然我只是在计时 Python 读取列表的速度。但这并没有真正改变我的问题。 所以,我前几天读了 this post 并想比较一下速度。我对 Pandas 还不熟悉,所以每当我看到有机会做一些比
假设这样一个数据 In [51]: data = list(range(10**6))
谁能解释一下为什么会这样? aatiis@aiur ~ $ time python /usr/lib64/python2.7/timeit.py -n 1 \ -- 'x = 10**1000
我是一名优秀的程序员,十分优秀!