Elapsed time in seconds in Python

If you'd like to measure and display the elapsed time during some operation in Python, probably the easiest way is to create a timestamp before and after the operation using time.time() and compute the difference.

In the following code the do_something is just some arbitrary function that takes slightly more than 1 seconds to run on my computer.

examples/elapsed_time_in_seconds.py

import time

def main():
    start = time.time()
    do_something()
    end = time.time()

    print(end - start)
    print(int(end - start))

def do_something():
    for a in range(50000000):
        b = a*a


main()

The result looks like this:

1.429948091506958
1

Handle big numbers

If the elapsed time is small then you'd be probably interested in a few digits after the decimal point, but if the elapsed time is long, lik 100 or 1000 seconds then you'd probably want a more Human readable format of the elapsed time