import time


class SimpleTimer(object):

    def __init__(self):
        self.reset()

    def now(self):
        return time.time()

    def elapsed(self):
        return self.now() - self.t0

    def reset(self):
        self.t0 = self.now()


class FPSCounter(object):

    def __init__(self):
        self.averageComputer = MovingAverageComputer()

    def tick(self):
        self.averageComputer.update(1)

    def getAverageFPS(self):
        return self.averageComputer.getAverage()


class AverageComputer(object):

    def __init__(self):
        self.timer = SimpleTimer()
        self.quantity = 0.0

    def update(self, quantitySinceLastUpdate):
        self.quantity += quantitySinceLastUpdate

    def getAverage(self):
        return self.quantity / self.timer.elapsed()

    def reset(self):
        self.quantity = 0.0
        self.timer.reset()


class MovingAverageComputer(object):

    def __init__(self):
        self.timer = SimpleTimer()
        self.alpha = 0.9
        self.timeWindow = 1.0
        self.average = 0.0
        self.quantityThisWindow = 0.0

    def update(self, quantitySinceLastUpdate):
        self.quantityThisWindow += quantitySinceLastUpdate
        self._updateAverage()

    def getAverage(self):
        self._updateAverage()
        return self.average

    def _updateAverage(self):

        elapsedTime = self.timer.elapsed()

        if elapsedTime > self.timeWindow:

            # compute FPS for this time window
            averageThisWindow = self.quantityThisWindow / elapsedTime

            # update moving average
            self.average = self.alpha * averageThisWindow + (1.0 - self.alpha) * self.average

            # reset
            self.timer.reset()
            self.quantityThisWindow = 0.0

@@ -350,66 +350,289 @@
 r()%0A
-%0A    def tick(self):%0A        self.averageComputer.update(1
+        self.printToConsole = False%0A%0A    def tick(self):%0A        newAverage =  self.averageComputer.timer.elapsed() %3E self.averageComputer.timeWindow%0A        self.averageComputer.update(1)%0A        if newAverage and self.printToConsole:%0A            print 'fps:', self.getAverageFPS(
 )%0A%0A 

add printing to FPSCounter