Code: Alles auswählen
#! /usr/bin/env python
# coding:utf-8
from __future__ import division
from timeit import Timer
REPEATS = 30000
# test, preparation, title
LOG10 = "int(log10( {0} )) + 1", "from math import log10", "log:"
LEN_STR = "len(str( {0} ))", "", "len-str:"
def main():
for i in (1, 1000, 1000000, 10**9, 2**50, 2**100):
print u"Zahl = {0}".format(i)
for test in (LOG10, LEN_STR):
t = Timer(test[0].format(i), test[1]).timeit(REPEATS)
print "{0:8}{1:12.6f}".format(test[2], t)
# Test:
#~ from math import log10
#~ t = eval (LOG10[0].format(i) + " == " + LEN_STR[0].format(i))
#~ print t
if "__main__" == __name__: main()
hth, Jörg (der sich endlich mit str.format auseinandergesetzt hat)