Fastest way to loop through each digit in a number?

Rune Strand rst at _nospam_.drlug.org._nospam_
Mon Sep 6 11:04:38 EDT 2004


On Mon, 6 Sep 2004 02:32:46 -0400, "Terry Reedy" <tjreedy at udel.edu>
wrote:

> You might also look at
>
>zero = ord('0')
>
>and then ord(i)-zero in loop

Thanks!  That seems like the fastest solution.
When looping through 1000000, I get these results:
ord     : 4.703  (Terry Reedy)
divmod  : 10.469 (Paul Rubin)
modulo  : 7.625  (Roy Smith)
lst comp: 11.750
map     : 9.062
str     : 8.219

The modulo and divmod methods includes list.append(d) and
list.reverse() (list.append mapped to list_append).




More information about the Python-list mailing list