Python 为什么IDLE 3.4在这个程序上花费这么长时间?

Python 为什么IDLE 3.4在这个程序上花费这么长时间?,python,python-idle,python-3.4,Python,Python Idle,Python 3.4,编辑:我正在完全重做这个问题。这个问题与时间无关 这里有一个程序: import time start=time.time() a=9<<(1<<26) # The line that makes it take a while print(time.time()-start) 我已经证实,当在Python2.7空闲状态下运行或从Python2.7或3.4上的命令行运行时,这个程序几乎是瞬时运行的 那么,Python3.4idle做了什么让它花费

编辑:我正在完全重做这个问题。这个问题与时间无关

这里有一个程序:

import time
start=time.time()
a=9<<(1<<26)             # The line that makes it take a while
print(time.time()-start)
我已经证实,当在Python2.7空闲状态下运行或从Python2.7或3.4上的命令行运行时,这个程序几乎是瞬时运行的


那么,Python3.4idle做了什么让它花费这么长时间呢?我知道计算这个数字并将其保存到内存是磁盘密集型的,但我想知道的是为什么Python3.4idle执行这个计算并编写,而Python2.7idle和命令行Python可能不这样做。

我会查看这一行并将其分开。你有:


9我真的很困惑。下面是3.4.1的更多结果。在3.4.1或3.3.5中从编辑器运行前两行中的任何一行都会产生相同的对比度

>>> a = 1 << 26; b = 9 << a  # fast, , .5 sec
>>> c = 9 << (1 << 26)  # slow, about 3 sec
>>> b == c  # fast
True
>>> exec('d=9<<(1<<26)', globals())  # fast
>>> c == d  # fast
True
a=1>c=9>b=c#快 真的
>>>exec('d=9对我来说不需要10秒,事实上,它的工作方式与预期的一样me@isaacg为什么你认为这个程序大约需要10秒?像这样的简单程序几乎不需要时间运行——例如,在我的2.7GHz mac mini上,“time python isaacg.py”报告运行大约需要0.028秒。您是否尝试过从终端运行此操作?是否仍有相同的结果?您可能希望运行多次并取平均值。请在文件中单独尝试此操作并从空闲状态运行:
def()A:对不起,但我不认为这是一个完整的答案。它不能解释为什么在我使用Python 3.4空闲时,而不是Python 2.7空闲或命令行时会出现这个观察到的时间延迟。我会把我的问题重述一下,使它更清晰。命令中的表达式是什么样子的?你是否考虑过秘密记忆的事实?不同的环境可能会出现参考问题?
In[73]: def create_number():
            return 9<<(1<<26)
In[74]: #Note that this seems instantaneous, but try calling the function!
In[75]: %timeit create_number()
#Python environment crashes because task is too hard
In[3]: from timeit import timeit
In[4]: timeit(setup = 'from __main__ import create_number', stmt = 'create_number()', number = 1)
Out[4]: .004942887388800443
In[107]: %timeit 9<<(1<<26)
10000000 loops, best of 3: 22.8 ns per loop

In[108]: def empty(): pass
In[109]: %timeit empty()
10000000 loops, best of 3: 96.3 ns per loop
>>> a = 1 << 26; b = 9 << a  # fast, , .5 sec
>>> c = 9 << (1 << 26)  # slow, about 3 sec
>>> b == c  # fast
True
>>> exec('d=9<<(1<<26)', globals())  # fast
>>> c == d  # fast
True