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 :

THE PANE METHOD FOR DEEP LEARNING!

Get your 7 DAYS FREE TRAINING to learn how to create your first ARTIFICIAL INTELLIGENCE!

For the next 7 days I will show you how to use Neural Networks.

You will learn what Deep Learning is with concrete examples that will stick in your head.

BEWARE, this email series is not for everyone. If you are the kind of person who likes theoretical and academic courses, you can skip it.

But if you want to learn the PANE method to do Deep Learning, click here :

Tom Keldenich
Tom Keldenich

Data Engineer & passionate about Artificial Intelligence !

Founder of the website Inside Machine Learning

Leave a Reply

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

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.