Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the execution time of the code?

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?

like image 861
Antony Avatar asked Dec 14 '22 22:12

Antony


2 Answers

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))
like image 69
Adeel Ahmad Avatar answered Dec 28 '22 05:12

Adeel Ahmad


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.

like image 32
Błotosmętek Avatar answered Dec 28 '22 07:12

Błotosmętek