PEP 378: Format Specifier for Thousands Separator

Carlos Nepomuceno carlosnepomuceno at outlook.com
Wed May 22 07:52:15 EDT 2013


----------------------------------------
> Date: Wed, 22 May 2013 07:25:13 -0400
> From: ned at nedbatchelder.com
[...]
> You have to keep in mind that 2.7 is not getting any new features, no
> matter how small they seem. If you create a patch that implements the
> comma flag in %-formatting, it *might* go into 3.x, but it will not go
> into 2.7.
>
> --Ned.

No problem. I have just discovered i was measuring the wrong thing.

My test case is been optimized at compile time by CPython that treats "'%d' % 12345" as a constant.
My use case is different because I almost have no literals been used with % operator.

So my gain isn't that great. In fact it's faster with str.format() than %, and it's even faster if I use the default format specifier.

C:\Python27>python -m timeit -cv -n10000000 -s"v=12345" "'%d'%v"
raw times: 10.5 10.7 10.7
10000000 loops, best of 3: 1.05 usec per loop

C:\Python27>python -m timeit -cv -n10000000 -s"v=12345" "'{:d}'.format(v)"
raw times: 8.11 8.09 8.02
10000000 loops, best of 3: 0.802 usec per loop

C:\Users\Josue\Documents\Python>python -m timeit -cv -n10000000 -s"v=12345" "'{}'.format(v)"
raw times: 5.3 5.5 5.62
10000000 loops, best of 3: 0.53 usec per loop

Using variables (100% of cases) makes str.format() 50% faster than %. 		 	   		  


More information about the Python-list mailing list