Live data from Hacker News

Fast Python loops

python.org

41–50 of 54 posts

Re: Fast Python loops

#41
post #32

Earlier quoted context omitted.

List comprehensions didn't exist when this post was written.

Python 2.0, back in 2000, had them.

The last sentence (grep for "since this essay was written") suggests that the article was written before the 'B' typecode was added to the array module.

This typecode was added in Python 1.5.

Re: Fast Python loops

#42
post #39

I think this is the most idiomatic way of doing it in modern python: > "".join(chr(x) for x in list_of_ints) This article is really really really out of date

The article is about performant python, not idiomatic python.

Re: Fast Python loops

#43
post #39

I think this is the most idiomatic way of doing it in modern python: > "".join(chr(x) for x in list_of_ints) This article is really really really out of date

The article is about performant python, not idiomatic python.

They are not mutually exclusive.

Re: Fast Python loops

#46
post #7

According to archive.org this essay is from at most June 2006, which would put it at Python 2.4 or earlier. The specific performance characteristics of Python will have changed greatly in the intervening period.

The last sentence (grep for "since this essay was written") suggests that the article was written before the 'B' typecode was added to the array module.

This typecode was added in Python 1.5.

Re: Fast Python loops

#48
post #47

In the time frame spent to test these hacks I would have written a perfect C module that runs circles around it.

Then realized it barfed on nulls, and started over with a more perfect implementation that took a bit longer.

Re: Fast Python loops

#49
I wish the Python community smoked its own shit: "There should be one-- and preferably only one --obvious way to do it." I have never observed either "one" or "obvious" in my dealings with the language.

Re: Fast Python loops

#50
post #34
post #6

The author states: "There's a general technique to avoid quadratic behavior in algorithms like this. I coded it as follows for strings of exactly 256 items:" def f5(list): string = "" for i in range(0, 256, 16): # 0, 16, 32, 48, 64, ... s = "" for character in map(chr, list[i:i+16]): s = s + character string = string + s return string I am not understanding what the technique is or why using a step size of 16 in the…

The idea is to reduce the amount of redundant copying of characters: you end up doing a few more concatenations in the outer loop, but the concatenations in the inner loop are of short strings. Importantly, if you remove the restriction of the input list being "exactly 256 items", then the method is still quadratic. A linear-time algorithm for this would copy each input character exactly once, which is effectively wh…

I see, that makes sense about reducing the constant. Interesting. Thanks for the great explanation.
Post reply on HN