Live data from Hacker News

Fast Python loops

python.org

1–10 of 54 posts

Re: Fast Python loops

#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 range function is significant. Can anyone enlighten me about this and what the technique is? Does this technique have a name?

Re: Fast Python loops

#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.

Re: Fast Python loops

#8
post #3

Interesting read! I'm still missing what "B" in the Python code referring to as well.

array.array isn't a normal python array. It's an optimized type/object where all the elements of the array are of the same type. The 'B' is a format string that indicates what type you want the elements to be. B == unsigned char

He's using it because it converts the int 65 to the string 'A', then toString() to join all the array elements together.

https://docs.python.org/2/library/array.html

See my other post though, it's not the fastest way to do this in python.

Re: Fast Python loops

#10
post #8
post #3

Interesting read! I'm still missing what "B" in the Python code referring to as well.

array.array isn't a normal python array. It's an optimized type/object where all the elements of the array are of the same type. The 'B' is a format string that indicates what type you want the elements to be. B == unsigned char He's using it because it converts the int 65 to the string 'A', then toString() to join all the array elements together. https://docs.python.org/2/library/array.html See my other post though,…

Great thanks! I have assume array.array is similar to 2-dimensional array.
Post reply on HN