# variables分配比一个class轮更快

``py -3 -m timeit "tuple(range(2000)) == tuple(range(2000))" 10000 loops, best of 3: 97.7 usec per loop py -3 -m timeit "a = tuple(range(2000)); b = tuple(range(2000)); a==b" 10000 loops, best of 3: 70.7 usec per loop` `

### 2 Solutions collect form web for “variables分配比一个class轮更快”

` `In [1]: %timeit -n10000 -r20 tuple(range(2000)) == tuple(range(2000)) 10000 loops, best of 20: 74.2 µs per loop In [2]: %timeit -n10000 -r20 a = tuple(range(2000)); b = tuple(range(2000)); a==b 10000 loops, best of 20: 75.7 µs per loop` `

` `% strace -o withoutvars python3 -m timeit "tuple(range(2000)) == tuple(range(2000))" 10000 loops, best of 3: 134 usec per loop % strace -o withvars python3 -mtimeit "a = tuple(range(2000)); b = tuple(range(2000)); a==b" 10000 loops, best of 3: 75.8 usec per loop % grep mmap withvars|wc -l 46 % grep mmap withoutvars|wc -l 41149` `

` `mmap(NULL, 262144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f32e56de000 munmap(0x7f32e56de000, 262144) = 0 mmap(NULL, 262144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f32e56de000 munmap(0x7f32e56de000, 262144) = 0 mmap(NULL, 262144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f32e56de000 munmap(0x7f32e56de000, 262144) = 0` `

`mmap`调用似乎来自`Objects/obmalloc.c`的函数`Objects/obmalloc.c` ; `obmalloc.c`也包含macros`ARENA_SIZE` ，它是`#define` d是`(256 << 10)` （即`262144` ）; `munmap``_PyObject_ArenaMunmap`中的`_PyObject_ArenaMunmap`匹配。

`obmalloc.c`

` `\$ python3 -m timeit "a = tuple(range(200000)); b = tuple(range(200000)); a is b" 100 loops, best of 3: 7.03 msec per loop \$ python3 -m timeit "a = tuple(range(200000)) is tuple(range(200000))" 100 loops, best of 3: 10.2 msec per loop \$ python3 -m timeit "tuple(range(200000)) is tuple(range(200000))" 100 loops, best of 3: 10.2 msec per loop \$ python3 -m timeit "a = b = tuple(range(200000)) is tuple(range(200000))" 100 loops, best of 3: 9.99 msec per loop \$ python3 -m timeit "a = b = tuple(range(200000)) is tuple(range(200000))" 100 loops, best of 3: 10.2 msec per loop \$ python3 -m timeit "tuple(range(200000)) is tuple(range(200000))" 100 loops, best of 3: 10.1 msec per loop \$ python3 -m timeit "a = tuple(range(200000)); b = tuple(range(200000)); a is b" 100 loops, best of 3: 7 msec per loop \$ python3 -m timeit "a = tuple(range(200000)); b = tuple(range(200000)); a is b" 100 loops, best of 3: 7.02 msec per loop` `

`a``b`分配添加到慢速版本不会加快速度。 事实上，我们可以预计分配给局部variables的影响可以忽略不计。 唯一能加快速度的是将expression式完全分成两部分。 这个唯一的区别就是在评估expression式（从4到3）的时候减less了Python使用的最大堆栈深度。

` `\$ python3 -m timeit -s "def foo(): tuple(range(200000)) is tuple(range(200000))" "foo()" 100 loops, best of 3: 10 msec per loop \$ python3 -m timeit -s "def foo(): tuple(range(200000)) is tuple(range(200000))" "foo()" 100 loops, best of 3: 10 msec per loop \$ python3 -m timeit -s "def foo(): a = tuple(range(200000)); b = tuple(range(200000)); a is b" "foo()" 100 loops, best of 3: 9.97 msec per loop \$ python3 -m timeit -s "def foo(): a = tuple(range(200000)); b = tuple(range(200000)); a is b" "foo()" 100 loops, best of 3: 10 msec per loop` `

• 寻找内置Python函数的源代码？
• Python的关机程序在哪里被logging？
• 为什么tuple（set（））== tuple（set（））85％的时间哈希随机化启用？
• 设置文字给出了设置函数调用的不同结果
• 为什么元组的内存空间less于列表？
• 为什么范围（0）==范围（2，2，2）在Python 3中为True？
• 元组是否比Python中的列表更有效？
• 什么时候del在python中有用？
• 为什么一个类的主体在定义时间被执行？
• 为什么这个Pythonstring的大小在一个失败的int转换上发生了变化
• 为什么硬编码这个列表比计算慢呢？