2017-07-03 81 views
1

我想计算各种语言的代码的执行时间,如java,python,javascript。如何获得这些代码的执行时间。是否有任何工具可用于python包或任何其他工具来通过传递文件(任何文件java或python)路径来计算执行时间。请分享您的建议。如何获取代码的执行时间?

我知道通过在Python代码中使用时间模块获取执行时间。如何在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) 

有没有其他方法可以实现这个目标?

回答

2

相反,其他的答案,我建议使用timeit,其目的是与测量考虑执行时间的根本目的,也可以作为一个独立的工具: https://docs.python.org/3/library/timeit.html

它不仅会给你执行的实时时间,而且会使用CPU时间,这不一定是同一件事情。

1
import time 
start_time = time.time() 

#code here 

print("--- %s seconds ---" % (time.time() - start_time)) 
0

你可以做到这一点使用time模块:

import time 
start_time = time.time() 
# your code 
end_time = time.time() 
print("Total execution time: {}".format(end_time - start_time)) 
0

我想你可能需要time模块。这是测量python执行时间的最简单方法。看看我的例子。

import time 
start_time = time.time() 
a=1 
for i in range(10000): 
    a=a+1 

end_time = time.time() 

total_time = end_time-start_time 

print("Execution time in seconds: %s ",total_time) 

输出:

Execution time in seconds: %s 0.0038547515869140625 
>>> 
相关问题