Created
April 23, 2015 11:38
-
-
Save okin/84024e46fc041dd2df80 to your computer and use it in GitHub Desktop.
Timing of various string formatting methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import string | |
import sys | |
import timeit | |
STRING_INTERPOLATION = "%s ist %i ist %.2f" | |
NAMED_STRING_INTERPOLATION = "%(letter)s ist %(ganzzahl)i ist %(dezimalzahl).2f" | |
STRING_FORMAT = "{0} ist {1} ist {2:.2f}" | |
NAMED_STRING_FORMAT = "{letter} ist {ganzzahl} ist {dezimalzahl:.2f}" | |
MIXED_STRING_FORMAT = "{letter} ist {0} ist {dezimalzahl:.2f}" | |
STRING_TEMPLATE = string.Template('$a ist $ganzzahl ist $dezimalzahl') | |
def stringInterpolation(): | |
"""String interpolation with %""" | |
return "%s ist %i ist %.2f" % ('a', 1, 1.0) | |
def stringInterpolationWithGlobalTemplate(): | |
"""String interpolation with % and global template""" | |
return "%s ist %i ist %.2f" % ('a', 1, 1.0) | |
def namedStringInterpolation(): | |
"""Named string interpolation with %""" | |
return "%(letter)s ist %(ganzzahl)i ist %(dezimalzahl).2f" % {'letter': 'a', 'ganzzahl': 1, 'dezimalzahl': 1.0} | |
def namedStringInterpolationWithGlobal(): | |
"""Named string interpolation with % and global template""" | |
return NAMED_STRING_INTERPOLATION % {'letter': 'a', 'ganzzahl': 1, 'dezimalzahl': 1.0} | |
def stringFormat(): | |
"""String.format""" | |
return "{0} ist {1} ist {2:.2f}".format('a', 1, 1.0) | |
def stringFormatWithGlobalTemplate(): | |
"""String.format with global template""" | |
return STRING_FORMAT.format('a', 1, 1.0) | |
def namedStringFormat(): | |
"""String.format with keyword arguments""" | |
return "{letter} ist {ganzzahl} ist {dezimalzahl:.2f}".format( | |
letter='a', | |
ganzzahl=1, | |
dezimalzahl=1.0 | |
) | |
def namedStringFormatWithGlobal(): | |
"""String.format with keyword arguments and global template""" | |
return NAMED_STRING_FORMAT.format( | |
letter='a', | |
ganzzahl=1, | |
dezimalzahl=1.0 | |
) | |
def mixedStringFormat(): | |
"""String.format with positional and keyword arguments""" | |
return "{letter} ist {0} ist {dezimalzahl:.2f}".format( | |
1, | |
letter='a', | |
dezimalzahl=1.0 | |
) | |
def mixedStringFormatWithGlobal(): | |
"String.format with positional and keyword arguments and global template" | |
return MIXED_STRING_FORMAT.format( | |
1, | |
letter='a', | |
dezimalzahl=1.0 | |
) | |
def stringTemplate(): | |
"""string.Template with local template""" | |
template = string.Template('$a ist $ganzzahl ist $dezimalzahl') | |
return template.substitute(a='a', ganzzahl=1, dezimalzahl=1.0) | |
def stringTemplateGlobalTemplate(): | |
"""string.Template with global template""" | |
return STRING_TEMPLATE.substitute(a='a', ganzzahl=1, dezimalzahl=1.0) | |
def main(runs=10000000): | |
print('Running under Python {0}'.format(sys.version)) | |
testedFunctions = ( | |
stringInterpolation, stringInterpolationWithGlobalTemplate, namedStringInterpolation, | |
stringFormat, stringFormatWithGlobalTemplate, namedStringFormat, namedStringFormatWithGlobal, mixedStringFormat, mixedStringFormatWithGlobal, | |
stringTemplate, stringTemplateGlobalTemplate | |
) | |
print("\nExample output:") | |
for func in testedFunctions: | |
print(': '.join((func.__doc__, func()))) | |
print("\n\ntiming {0} runs:".format(runs)) | |
for func in testedFunctions: | |
print( | |
"{doc}: {t} seconds".format( | |
doc=func.__doc__, | |
t=timeit.timeit(func, number=runs) | |
) | |
) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment