'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
Solution 1:[1]
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))
Solution 2:[2]
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.
Solution 3:[3]
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}")
Solution 4:[4]
For finding your function run time prefer time.perf_counter() over time.time(). See the below link for details
Understanding time.perf_counter() and time.process_time()
You can create your own custom timer using something like this
from time import perf_counter
def f(a1,a2):
return a1 * a2
def timer(f,*args):
start = perf_counter()
f(*args)
return (1000 * (perf_counter()-start)) # this returns time in ms
a1 = np.random.rand(100)
a2 = np.random.rand(100)
np.mean([timer(f,a1,a2) for _ in range(100)]) # average out result for 100 runs
If you are using jupyter notebook use the following
%%timeit
f(a1,a2)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Maeaex1 |
Solution 2 | Axois |
Solution 3 | marc_s |
Solution 4 | Abhishek Sharma |