How to get the execution time of the code?

Antony picture Antony · Jul 3, 2017 · Viewed 11.3k times · Source

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?

Answer

Adeel Ahmad picture Adeel Ahmad · Jul 3, 2017

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))