output from external commands

Fredrik Lundh fredrik at pythonware.com
Mon Oct 24 23:14:22 CEST 2005

Terry Hancock wrote:

> Note also that for those who count, "str(f)" is exactly as long
> (in keystrokes) as "'%s'%f", making the "just" a matter of opinion.

the % implementation still has to create an overallocated output buffer,
parse the format string, call str() on the argument, verify the result,
check that the new string fits in the output buffer, copy the contents
to the new string to the output buffer, discard the new string, and
finally, when the entire format string has been processed, resize the
output buffer to the right size.

however, the memory allocator is fast, block copies are cheap, and
operator access is faster than global function accesses, so as long
as the format string is short, the resulting string is no more than 100
bytes longer, and Raymond hasn't gotten around to optimize the str()
code path, % can be slightly faster than a plain call to str().  in all
other cases, str() is faster.

(using either on the output from glob.glob is just plain silly, of course)


More information about the Python-list mailing list