[Python] 纯文本查看 复制代码
import timeit
def main():
l_align = 25
print(f'{"1、while 循环":<{l_align}} {timeit.timeit(while_loop, number=1):.6f}')
print(f"{'2、for 循环':<{l_align}} {timeit.timeit(for_loop, number=1):.6f}")
print(f'{"3、sum range":<{l_align}} {timeit.timeit(sum_range, number=1):.6f}')
print(f'{"4、sum generator":<{l_align}} {timeit.timeit(sum_generator, number=1):.6f}')
print(f'{"5、sum list comprehension":<{l_align}} {timeit.timeit(sum_list_comp, number=1):.6f}')
print(f'{"6、sum numpy":<{l_align}} {timeit.timeit(sum_numpy, number=1):.6f}')
print(f'{"7、sum numpy python range":<{l_align}} {timeit.timeit(sum_numpy_python_range, number=1):.6f}')
if __name__ == '__main__':
main()