5 ms·
Could someone please explain #Test 11. How is using %s instead of %d double as fast even though it is the same thing is being calculated? I always use %d becaus
by jwl 12y ago
Could someone please explain #Test 11. How is using %s instead of %d double as fast even though it is the same thing is being calculated? I always use %d because it seems like the prooper way to do it.
- nikital 12y agoI looked at the source of 2.7: The function PyString_Format (Objects/stringobject.c) does the formatting for the % operator. For %s it calls _PyObject_Str which in turn calls "str()" on the object. For %d it calls formatint (located in the same file). The str() implementation for ints is in int_to_decimal_string (Objects/intobject.c) and it's incredibly simple: do { *--p = '0' + (char)(absn % 10); absn /= 10; } while (absn); The code for formatint is way more complex, and it contains two call to the native snprintf: PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", sign, (flags&F_ALT) ? "#" : "", prec, type); // ... PyOS_snprintf(buf, buflen, fmt, -x); The native snprintf is heavier because it handles precision, zero-padding and stuff like that. I believe this is why %d is slower. %s is a straight "devide-by-10-and-subtract" loop while %d are two library calls to the full-blown sprintf. However I didn't actually profile the code because I don't have a debug build, so I might be completely wrong. Edit: Link to code: https://github.com/python/cpython/blob/a5c7a4257507a77699a1ab8ec079f41d505b9eee/Objects/stringobject.c#L4444 https://github.com/python/cpython/blob/a5c7a4257507a77699a1a...