Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print iterations per second?

I have a small Python script which sends POST requests to a server and gets their response.

It iterates 10000 times, and I managed to print the current progress in command prompt using:

code=current_requestnumber
print('{0}/{1}'.format(str(code),"10000"),end="\r")

at the end of each loop.

Because this involves interaction with a webserver, I would like to show the current average speed next to this too (updated like every 2 seconds).

An example at the bottom of the command prompt would then be like this:

(1245/10000), 6.3 requests/second

How do I achieve this?

like image 455
user2879551 Avatar asked Jun 14 '15 21:06

user2879551


1 Answers

You can use the tqdm library for this. A simple example for this is

from tqdm import tqdm
for i in tqdm(range(1e20)):
    ##LOOP BODY

This will print the current iteration, iterations/second, ETA and a nice progress bar

e.g.

 21%|████████████████████  21/100 [01:45<04:27,  3.39s/it]

like image 164
Het Thakkar Avatar answered Oct 16 '22 20:10

Het Thakkar