How can I know in Python how many times a line of code is executed per minute?
<p>For example if I have:  </p>

<pre><code> something=something+1 
</code></pre>

<p>I want to know how many times in a minute, this line is executed in order to create another variable with this result? </p>

<p>For benchmarking you would probably ask for per second timings.</p>

<p>To count events in the last minute, here is a class remembering event timestamps for a given period:</p>

<pre><code>import bisect
import time

class TimedCounter:
    def __init__(self, period=60.0):
        self._timestamps = []
        self._period = period

    def _cleanup(self, now):
        del self._timestamps[:bisect.bisect_left(self._timestamps, now - self._period)]

    def increment(self):
        now = time.time()
        self._timestamps.append(now)
        self._cleanup(now)   # optimization: not necessary to call every time

    def get_count(self):
        self._cleanup(time.time())
        return len(self._timestamps)
</code></pre>

<p>and an example:</p>

<pre><code>tc = TimedCounter(1.0)
for i in range(7, 27):
    tc.increment()
    print("{} time(s) in the last second".format(tc.get_count()))
    time.sleep(7/i)
</code></pre>

python loops time