percent faster than format()? (was: Re: optomizations)
Am 23.04.2013 06:00, schrieb Steven D'Aprano: If it comes down to micro-optimizations to shave a few microseconds off, consider using string % formatting rather than the format method. Why? I don't see any obvious difference between the two... Greetings! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: percent faster than format()? (was: Re: optomizations)
On Tue, Apr 23, 2013 at 9:46 AM, Ulrich Eckhardt ulrich.eckha...@dominolaser.com wrote: Am 23.04.2013 06:00, schrieb Steven D'Aprano: If it comes down to micro-optimizations to shave a few microseconds off, consider using string % formatting rather than the format method. Why? I don't see any obvious difference between the two... Greetings! Uli -- http://mail.python.org/mailman/listinfo/python-list $ python -m timeit a = '{0} {1} {2}'.format(1, 2, 42) 100 loops, best of 3: 0.824 usec per loop $ python -m timeit a = '%s %s %s' % (1, 2, 42) 1000 loops, best of 3: 0.0286 usec per loop -- Kwpolska http://kwpolska.tk | GPG KEY: 5EAAEA16 stop html mail| always bottom-post http://asciiribbon.org| http://caliburn.nl/topposting.html -- http://mail.python.org/mailman/listinfo/python-list
Re: percent faster than format()? (was: Re: optomizations)
On Tue, 23 Apr 2013 09:46:53 +0200, Ulrich Eckhardt wrote: Am 23.04.2013 06:00, schrieb Steven D'Aprano: If it comes down to micro-optimizations to shave a few microseconds off, consider using string % formatting rather than the format method. Why? I don't see any obvious difference between the two... Possibly the state of the art has changed since then, but some years ago % formatting was slightly faster than the format method. Let's try it and see: # Using Python 3.3. py from timeit import Timer py setup = a = 'spam'; b = 'ham'; c = 'eggs' py t1 = Timer('%s, %s and %s for breakfast' % (a, b, c), setup) py t2 = Timer('{}, {} and {} for breakfast'.format(a, b, c), setup) py print(min(t1.repeat())) 0.8319804421626031 py print(min(t2.repeat())) 1.2395259491167963 Looks like the format method is about 50% slower. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: percent faster than format()? (was: Re: optomizations)
On Wed, Apr 24, 2013 at 12:36 AM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: # Using Python 3.3. py from timeit import Timer py setup = a = 'spam'; b = 'ham'; c = 'eggs' py t1 = Timer('%s, %s and %s for breakfast' % (a, b, c), setup) py t2 = Timer('{}, {} and {} for breakfast'.format(a, b, c), setup) py print(min(t1.repeat())) 0.8319804421626031 py print(min(t2.repeat())) 1.2395259491167963 Looks like the format method is about 50% slower. Figures on my hardware are (naturally) different, with a similar (but slightly more pronounced) difference: sys.version '3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)]' print(min(t1.repeat())) 1.4841416995735415 print(min(t2.repeat())) 2.5459869899666074 t3 = Timer(a+', '+b+' and '+c+' for breakfast', setup) print(min(t3.repeat())) 1.5707538248576327 t4 = Timer(''.join([a, ', ', b, ' and ', c, ' for breakfast']), setup) print(min(t4.repeat())) 1.5026834416105999 So on the face of it, format() is slower than everything else by a good margin... until you note that repeat() is doing one million iterations, so those figures are effectively in microseconds. Yeah, I think I can handle a couple of microseconds. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: percent faster than format()?
Am 23.04.2013 10:26, schrieb Chris “Kwpolska” Warrick: On Tue, Apr 23, 2013 at 9:46 AM, Ulrich Eckhardt ulrich.eckha...@dominolaser.com wrote: Am 23.04.2013 06:00, schrieb Steven D'Aprano: If it comes down to micro-optimizations to shave a few microseconds off, consider using string % formatting rather than the format method. Why? I don't see any obvious difference between the two... [...] $ python -m timeit a = '{0} {1} {2}'.format(1, 2, 42) 100 loops, best of 3: 0.824 usec per loop $ python -m timeit a = '%s %s %s' % (1, 2, 42) 1000 loops, best of 3: 0.0286 usec per loop Well, I don't question that for at least some CPython implementations one is faster than the other. I don't see a reason why one must be faster than the other though. In other words, I don't understand where the other one needs more time to achieve basically the same. To me, the only difference is the syntax, but not greatly so. So again, why is one faster than the other? What am I missing? Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: percent faster than format()?
Ulrich Eckhardt ulrich.eckha...@dominolaser.com writes: So again, why is one faster than the other? What am I missing? The .format() syntax is actually a function, and that alone carries some overload. Even optimizing the lookup may give a little advantage: from timeit import Timer setup = a = 'spam'; b = 'ham'; c = 'eggs' t1 = Timer('%s, %s and %s for breakfast' % (a, b, c), setup) t2 = Timer('{}, {} and {} for breakfast'.format(a, b, c), setup) print(min(t1.repeat())) print(min(t2.repeat())) setup = a = 'spam'; b = 'ham'; c = 'eggs'; f = '{}, {} and {} for breakfast'.format t3 = Timer(f(a, b, c), setup) print(min(t3.repeat())) 0.3076407820044551 0.44008257299719844 0.418146252995939 But building the call frame still takes its bit of time. ciao, lele. -- nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia. l...@metapensiero.it | -- Fortunato Depero, 1929. -- http://mail.python.org/mailman/listinfo/python-list