I want to calculate the execution time of code of various languages such as java, python, javascript. How to get the execution time of these codes. Is there any tool available in python packages or any other to calculate the execution time by passing the file(any file java or python) path. Please share your suggestion.
I am aware of getting execution time by using time module in python code. How to execute Javascript and java codes in python and get the execution time in common function.
I tried in below method.
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)
Is there any other method available to achieve this?
You can do that using the time
module:
import time
start_time = time.time()
# your code
end_time = time.time()
print("Total execution time: {} seconds".format(end_time - start_time))
Contrary to other answers, I suggest using timeit
, which was designed with the very purpose of measuring execution times in mind, and can also be used as a standalone tool: https://docs.python.org/3/library/timeit.html
It will give you not only the real time of execution, but also CPU time used, which is not necessarily the same thing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With