Performance Comparison - C++/Java/Python/Ruby/Jython/JRuby/Groovy (2008)
blog.dhananjaynene.com
blog.dhananjaynene.com
Language implementations are optimised for the most common operations in that language. Allowing only one implementation to take advantage of this renders the benchmark useless.
The real Python code I'd use to implement the defined algorithm would be a fifth of the size and running time (7 lines, sans whitespace, runs in 16.4 µs versus 92.7 µs under his timing scheme (it should really use min/timeit, not average/custom-timing-solution)).
import collections
def kill(size, nth):
chain = collections.deque(reversed(xrange(size)))
while len(chain) > 1:
chain.rotate(nth)
chain.popleft()
return chain.pop() def kill(size, nth):
lst = range(size)
offset = nth-1
len_ = len(lst)
while len_ > 1:
del lst[offset::nth]
offset = (offset - len_)% nth
len_ = len(lst)
return lst[0]
It will reduce the list by 1/3rd each pass, so it's significantly faster at larger sizes.I was too lazy to figure out the slicing solution myself. :)
def method2:
chain = [Person(x) for x in xrange(i)]
chain = chain[:2] + chain[4:]
which is 4x faster: timeit method1: 23.7072529793
timeit method2: 5.78906202316On the question "who whould want to use", here's an example:
OrderedSet could be implemented using double-linked list http://code.activestate.com/recipes/576696-orderedset-with-w...
Here's "real code" from stdlib (OrderedDict implementation) http://svn.python.org/view/python/trunk/Lib/collections.py?v...
Claim looks unfounded. (unless you can tell what looks buggy)
I'm not sure why they used a D-L list, but thanks for the link - I'll try to reimplement it with standard lists in a bit and run some bigger benchmark on it. Maybe collections need some tweaking? (I'm not saying list is always faster, only that it is for this specific example)