Python循环速度源

标签 python performance list if-statement append

假设我有一个 1000 万行的输入。 我想知道执行此操作需要多长时间:

if line not in list:
  list.append(line)

是否有任何网站或其他来源可以告诉我各种任务的大概速度?

最佳答案

你可以得到你的程序的总运行时间:

time python3 program.py

在您的终端中。它的输出类似于:

real    0m0.184s
user    0m0.032s
sys     0m0.015s

你可以得到一个特定函数运行 n 次需要多长时间:

from timeit import timeit

def foo():
    return 123456789 + 987654321

n = 10000000
time = timeit(foo, number=n)
print(time)

您还可以用time 计算一段代码花费的时间:

from time import time

start = time()

# code to be timed

finish = time()
print(finish - start)

关于Python循环速度源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25606852/

相关文章:

python - 如何在 psycopg2 中返回列表而不是元组

python - 如何在python中获取锯齿状数组的长度

python - 如何使用字典扫描列表中的部分外观?

python - Python 解释器报告异常时给出意外行

性能调整 Hive 查询

java - 比双重嵌套的 ArrayList 更有效?

javascript - 是否有必要减少 phonegap 应用程序对本地资源的请求?

Python 父/子类方法调用

python - 大型二维掩码数组的插值

python - elasticsearch批量方法失败,并带有字母数字ID