""" x**0.5 math.pow(x,0.5) 哪个快 用符号更快 by_symbol(),avg:0.012180216483431212 by_math(),avg:0.04381652316952369 """ import math from tool.timeit import test_commands def by_symbol(): return 2 ** 0.5 def by_math(): return math.pow(2, 0.5) if __name__ == '__main__': commands = [ "by_symbol()", "by_math()", ] test_commands(commands, setup="from __main__ import by_symbol,by_math")
""" x**2 x*x 哪个快 后者快 2**2,avg:0.0025651156002803027 2*2,avg:0.0020352332593166407 """ from tool.timeit import test_commands if __name__ == '__main__': commands = [ "2**2", "2*2", ] test_commands(commands, repeat=1000)
"""正常的 str 与 空内插 print('test') print(f'test') 因为 print 需要输出,所以只测试赋值语句 测试发现差距不大,只有当 f'' 中需要计算时才会有差距 """ from tool.timeit import test_commands if __name__ == '__main__': commands = ["a='test'", "b=f'test'", "c=f'test{1+2}'"] test_commands(commands)