Fastest way to count your iterations?

Tracy Ruggles tracer at axiomfire.com
Mon Jul 28 20:41:42 CEST 2003


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? ]

--T




More information about the Python-list mailing list