Get the execution time of your Python code easily

In Python it is interesting to display the execution time of your code, it gives an idea of the speed of your algorithm.

That’s why we will see in this article how to do it !

Execution time

Raw format

First of all let’sdisplay the execution time in raw format.

For that we use the time package.

In fact, we will simply store the time it is when we start our algorithm.

Then, the algorithm is executed (here we chose to repeat the square of i).

Then we subtract the current time from the time we had stored :

import time

start = time.time()

for i in range(10000):
  i**i

print(time.time() - start)

Output : 6.517595529556274

This simply gives us the time that has elapsed between the start and the end of the execution.

Here, 6.5 seconds.

HH:MM:SS format

To have a more intuitive format, we may also use the ctime() function.

This function will simply allow us to convert the raw format into date & time format.

Once this date & time format is obtained, we extract solely the hours, minutes and seconds.

This gives us :

import time

start = time.time()

for i in range(10000):
  i**i

print(time.ctime(time.time() - start)[11:19])

Output : 00:00:06

Personally, I find the latter format easily understandable and favor it in my Machine Learning algos.

Take the one that best suit your needs !

That’s all for this article. Feel free to check our other tips for Python developers.

sources :

One last word, if you want to go further and learn about Deep Learning - I've prepared for you the Action plan to Master Neural networks. for you.

7 days of free advice from an Artificial Intelligence engineer to learn how to master neural networks from scratch:

  • Plan your training
  • Structure your projects
  • Develop your Artificial Intelligence algorithms

I have based this program on scientific facts, on approaches proven by researchers, but also on my own techniques, which I have devised as I have gained experience in the field of Deep Learning.

To access it, click here :

GET MY ACTION PLAN

GET MY ACTION PLAN

Tom Keldenich
Tom Keldenich

Artificial Intelligence engineer and data enthusiast!

Founder of the website Inside Machine Learning

Leave a Reply

Your email address will not be published. Required fields are marked *

This page will not stay online forever

Enter your email to receive for free

The PANE method for Deep Learning

* indicates required

 

You will receive one email per day for 7 days – then you will receive my newsletter.
Your information will never be given to third parties.

You can unsubscribe in 1 click from any of my emails.



Entre ton email pour recevoir gratuitement
la méthode PARÉ pour faire du Deep Learning


Tu recevras un email par jour pendant 7 jours - puis tu recevras ma newsletter.
Tes informations ne seront jamais cédées à des tiers.

Tu peux te désinscrire en 1 clic depuis n'importe lequel de mes emails.