Ant*_*ony 4 python performance-testing execution-time
我想计算java、python、javascript等各种语言的代码执行时间。如何获取这些代码的执行时间。python 包或任何其他工具中是否有任何工具可以通过传递文件(任何文件 java 或 python)路径来计算执行时间。请分享您的建议。
我知道通过在 python 代码中使用 time 模块来获取执行时间。如何在python中执行Javascript和java代码并获取常用函数的执行时间。
我在下面的方法中尝试过。
import time
def get_exectime(file_path): # pass path of any file python,java,javascript, html, shell
start_time=time.time()
# execute the file given here. How to execute all file types here?
end_time=time.time()
exec_time=end_time-start_time
print(exec_time)
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以实现这一目标?
你可以使用time模块来做到这一点:
import time
start_time = time.time()
# your code
end_time = time.time()
print("Total execution time: {} seconds".format(end_time - start_time))
Run Code Online (Sandbox Code Playgroud)
与其他答案相反,我建议使用timeit,它的设计目的就是为了测量执行时间,也可以用作独立工具:https ://docs.python.org/3/library/timeit.html
它不仅会给你实时的执行时间,还会给你使用的CPU时间,这不一定是同一件事。
| 归档时间: |
|
| 查看次数: |
11830 次 |
| 最近记录: |