Fastest way to count your iterations?

Andrew Dalke adalke at mindspring.com
Mon Jul 28 15:08:41 EDT 2003


Tracy Ruggles:
> 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)...

What you wrote is very standard, so I would suggest using it.

However, if you want performance, try variations on

def counter(items, f, sep=1000):
  next_item = iter(items).next
  values = range(sep)
  for i in range(0, sys.maxint, sep):
    for j in values:
      f(next_item())
    print i+sep+1


                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list