python - How to print iterations per second? -


i have small python script sends post requests server , gets response.

it iterates 10000 times, , managed print current progress in command prompt using:

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

at end of each loop.

because involves interaction webserver, show current average speed next (updated every 2 seconds).

an example @ bottom of command prompt this:

(1245/10000), 6.3 requests/second

how achieve this?

you can total average number of events per second this:

#!/usr/bin/env python3  import time import datetime dt  start_time = dt.datetime.today().timestamp() = 0 while(true):     time.sleep(0.1)     time_diff = dt.datetime.today().timestamp() - start_time     += 1     print(i / time_diff) 

which in example print approximately 10. please note used timestamp method of datetime availble in python 3.

now, if calculate "current" number of events per second, on last 10 events, can this:

#!/usr/bin/env python3  import time import datetime dt  last_time = dt.datetime.today().timestamp() diffs = [] while(true):     time.sleep(0.1)      # add new time diff list     new_time = dt.datetime.today().timestamp()     diffs.append(new_time - last_time)     last_time = new_time      # clip list     if len(diffs) > 10:         diffs = diffs[-10:]      print(len(diffs) / sum(diffs)) 

here, i'm keeping list of durations of last 10 iterations on can use average number of events per second.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -