How to print iterations per second?
Question: 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?


Answer: You can get a total average number of events per second like this:

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

Which in this example would print approximately 10. Please note that I used a
`timestamp` method of `datetime` which is only availble in Python 3.

Now, if you would like to calculate the "current" number of events per second,
say over the last 10 events, you can do it like this:

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

Here, I'm keeping a list of durations of last 10 iterations over which I can
then use to get the average number of events per second.

