Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How long does my Python application take to run?

Tags:

python

timer

Basically, I want to be able to output the elapsed time that the application is running for. I think I need to use some sort of timeit function but I'm not sure which one. I'm looking for something like the following...

START MY TIMER code for application more code more code etc STOP MY TIMER 

OUTPUT ELAPSED TIME to do the above code in between start and stop of timer. Thoughts?

like image 921
user1675111 Avatar asked Sep 16 '12 02:09

user1675111


People also ask

Why does my Python program take so long to run?

Why is Python slow? The default implementation of Python 'CPython' uses GIL (Global Interpreter Lock) to execute exactly one thread at the same time, even if run on a multi-core processor as GIL works only on one core regardless of the number of cores present in the machine.

How long can a Python script run?

This can literally run forever. The Python garbage collector will free the memory used by A when it's value is overwritten.


1 Answers

The simplest way to do it is to put:

import time start_time = time.time() 

at the start and

print "My program took", time.time() - start_time, "to run" 

at the end.

like image 176
David Robinson Avatar answered Oct 14 '22 15:10

David Robinson