其他分享
首页 > 其他分享> > timeit测试函数执行时间

timeit测试函数执行时间

作者:互联网

def list_append():
  l = []
  for i in range(5000):
    l.append(i)

def list_insert():
  l = []
  for i in range(5000):
    l.insert(0, i)

import timeit

# 默认执行1万次
append_spent = timeit.timeit(
  setup='from __main__ import list_append',
  stmt='list_append()',
  number=10000,
)
print('list_append:', append_spent)

insert_spent = timeit.timeit(
  setup='from __main__ import list_insert',
  stmt='list_insert()',
  number=10000,
)
print('list_insert:', insert_spent)


标签:__,insert,timeit,list,spent,测试函数,执行,append
来源: https://www.cnblogs.com/weiweivip666/p/16660325.html