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

timeit测试函数执行时间

时间:2022-09-06 04:11:06浏览次数:67  
标签:__ insert timeit list spent 测试函数 执行 append

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
From: https://www.cnblogs.com/weiweivip666/p/16660325.html

相关文章