Last month I was working on a machine learning project. If you make use of grid search to find the optimum parameters, it is nice to know how much time an iterating process costs, so I do not waste my time. In this blog you’ll learn how to:

Install the progress bar library in Windows

The disadvantage of the progress bar library

Write your own progress timer class in Python

Use three lines of code to time your code progress

Install the progress bar library in Windows

Data Science life can be easy sometimes. If you make use of Anaconda (I strongly recommend) then it is the following line of code in de command line:

conda install progressbar

Otherwise you could you do the trick by:

pip install progressbar

The disadvantage of the progress bar library

The progress bar library is really nice, but the progress bar library has metrics that are easy to forget. Documentation can be found here. Each time I would like to time my code progress, I simply like to initialize, start and finish the job. Now you need for example to initialize the widgets of the progress bar and define the timer. The progress library works as follows:

#import libraries import progressbar as pb #initialize widgets widgets = ['Time for loop of 1 000 000 iterations: ', pb.Percentage(), ' ', pb.Bar(marker=pb.RotatingMarker()), ' ', pb.ETA()] #initialize timer timer = pb.ProgressBar(widgets=widgets, maxval=1000000).start() #for loop example for i in range(0,1000000): #update timer.update(i) #finish timer.finish()

Time for loop of 1 000 000 iterations: 100% |||||||||||||||||||||||| Time: 0:00:07

Write your own progress timer class in Python

Therefore it is easier to write your own class that you can easily use multiple times. First you initialize the progress timer. From now on, when you initialize you only need to give information how many iterations the code takes. You will also get the opportunity to add a description to the progress timer.

#import libraries import progressbar as pb #define progress timer class class progress_timer: def __init__(self, n_iter, description="Something"): self.n_iter = n_iter self.iter = 0 self.description = description + ': ' self.timer = None self.initialize() def initialize(self): #initialize timer widgets = [self.description, pb.Percentage(), ' ', pb.Bar(marker=pb.RotatingMarker()), ' ', pb.ETA()] self.timer = pb.ProgressBar(widgets=widgets, maxval=self.n_iter).start() def update(self, q=1): #update timer self.timer.update(self.iter) self.iter += q def finish(self): #end timer self.timer.finish()

Three lines of code to time your code progress

Now it is simple to show the progress of your code. You only need three lines of code to initialize, update and finish the progress timer. In case of a for loop it works as follows:

#initialize pt = progress_timer(description= 'For loop example', n_iter=1000000) #for loop example for i in range(0,1000000): #update pt.update() #finish pt.finish()

So you will receive feedback in how much time your function or for loop is finished.

Time for loop of 1 000 000 iterations: 37% |//////// | ETA: 0:00:04

And finally you will see how much time it took to do the job.

Time for loop of 1 000 000 iterations: 100% |||||||||||||||||||||||| Time: 0:00:07

I hope this will help you to perfectly time when to take a coffee. I'm sure there is a lot to improve in this approach, and maybe you got a better way to do execution time tracking. I'd like to hear your ideas!