How to get the execution time of the code?

Viewed 14090

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?

5 Answers

First install "humanfriendly" package in python by opening Command Prompt (CMD) as administrator and type - pip install humanfriendly

Code:

from humanfriendly import format_timespan
import time
begin_time = time.time()
# Put your code here
end_time = time.time() - begin_time
print("Total execution time: ", format_timespan(end_time))

Output:

enter image description here

Related