Skip to content

Instantly share code, notes, and snippets.

View smarnach's full-sized avatar
🦀

Sven Marnach smarnach

🦀
  • Mozilla
  • Preetz, Germany
View GitHub Profile
Python 2.7.2+ (default, Jun 29 2011, 00:07:42)
[GCC 4.6.1 20110616 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class A(object):
... pass
...
>>> A.__doc__ = "doc"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: attribute '__doc__' of 'type' objects is not writable
>>> timeit.repeat("a = deque()\nfor i in xrange(10000): a.append(i)\nfor x in a: x",
... "from collections import deque", number=1000)
[1.4415068626403809, 1.4344120025634766, 1.4339849948883057]
>>> timeit.repeat("a = []\nfor i in xrange(10000): a.append(i)\nfor x in a: x", number=1000)
[1.3635458946228027, 1.3510761260986328, 1.3518819808959961]
>>> timeit.repeat("for i in xrange(1000): a.append(i)",
... "from collections import deque; a = deque()", number=10000)
[1.139456033706665, 1.1287789344787598, 1.129647970199585]
>>> timeit.repeat("for i in xrange(1000): a.append(i)", "a = []", number=10000)
[1.1056618690490723, 1.0919761657714844, 1.0903148651123047]
In [1]: %timeit sum([(0, 0)] * 1000, ())
100 loops, best of 3: 5.01 ms per loop
In [2]: %timeit sum([(0, 0)] * 10000, ())
1 loops, best of 3: 490 ms per loop
In [3]: %timeit sum([(0, 0)] * 100000, ())
1 loops, best of 3: 61.9 s per loop
def compose(*funcs):
def comp(x):
for f in reversed(funcs):
x = f(x)
return x
return comp
@smarnach
smarnach / gist:784475
Created January 18, 2011 14:10
Be careful with object identity of immutable objects in Python (CPython 2.6.6)
In [1]: a = 256
In [2]: b = 256
In [3]: a is b
Out[3]: True
In [4]: a = 257
In [5]: b = 257
import timeit
a = range(10000)
da = set(a)
def list_first():
return 0 in a
def set_first():
return 0 in da