Fastest way to count your iterations?

Erik Max Francis max at alcyone.com
Mon Jul 28 20:57:05 CEST 2003


Tracy Ruggles wrote:

> If you're writing a simple script and you want to give an update of
> its progress...  Is this the fastest way to do it (assuming that what
> you're iterating through is very, very long and the time to process
> each item is relatively short)...
> 
> <code>
> 
> def counter(items, f, sep=1000):
>         i = 0
>         for item in items:
>                 f(item)
>                 i += 1
>                 if i % sep == 0:
>                         print i
> 
> </code>
> 
> [ is the modulus operator the quickest way to find out if you're
> really at the 1000th item? ]

The modulus operator is typically pretty expensive, though I don't know
with the object creation overhead in Python it would be all that
significant (I haven't checked).  If you don't need to keep track of the
total count (that is, you're just maintaining i for the purposes of
determining when it is a multiple of sep), then you can simply count i
up to sep and then reset it:

	i = 0
	for ...:
	    i += 1
	    if i == sep:
	        ... do something every `step' iterations ...
	        i = 0

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ I would rather understand one cause than be king of Persia.
\__/  Democritus




More information about the Python-list mailing list