Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I see the time it took to run my program in Visual Studio Code?

Is there a way to see how long a script took to execute/complete in VS Code?

I'm looking for a message like:

Program finished in 30ms
like image 698
Niel Agneessens Avatar asked Aug 08 '19 09:08

Niel Agneessens


3 Answers

Use 'time'

When your script starts:

import time
start_time = time.time()

do something # here your actual code/routine

print("Process finished --- %s seconds ---" % (time.time() - start_time))
like image 148
Maeaex1 Avatar answered Oct 09 '22 13:10

Maeaex1


You can create a simple decorator function to time your functions.

import time

def decoratortimer(decimal):
    def decoratorfunction(f):
        def wrap(*args, **kwargs):
            time1 = time.monotonic()
            result = f(*args, **kwargs)
            time2 = time.monotonic()
            print('{:s} function took {:.{}f} ms'.format(f.__name__, ((time2-time1)*1000.0), decimal ))
            return result
        return wrap
    return decoratorfunction

@decoratortimer(2)
def callablefunction(name):
    print(name)
print(callablefunction('John'))

I suggest using time.monotonic(which is a clock that doesnt go backwards) to increase the accuracy.

like image 3
Axois Avatar answered Oct 09 '22 15:10

Axois


Easiest way to achieve this is by purely coding the time to program.

from time import time

start_time = time()

main() # n staffs

passed_time = time() - start_time

print(f"It took {passed_time}")
like image 1
vahvero Avatar answered Oct 09 '22 13:10

vahvero