# Pygame time.get_fps() value

I was looking at the pygame.time.Clock.get_fps() method and after puting it inside of the while loop printing and checking it some times, i see that i starts to give its value after five frames (and not 10 as said in the documentation). I did put this inside the while loop (clock is the object of the pygame.time.Clock() class created inside my class):

 print "clock.tick:", self.clock.tick(self.fps)
print "clock.get_fps", self.clock.get_fps()


and the result by having the fps=60 was:

clock.tick: 48
clock.get_fps 0.0
clock.tick: 16
clock.get_fps 0.0
clock.tick: 16
clock.get_fps 0.0
clock.tick: 16
clock.get_fps 0.0
clock.tick: 16
clock.get_fps 0.0
clock.tick: 16
clock.get_fps 51.8134689331
clock.tick: 16
clock.get_fps 51.8134689331
clock.tick: 16
clock.get_fps 51.8134689331
clock.tick: 16
clock.get_fps 51.8134689331
clock.tick: 16
clock.get_fps 51.8134689331


As one can see the first clock.get_fps comes out after the first 5 calls, but it supposedly should calculate the average of the last 10 calls to tick(). What am i missing here? Can anyone help explain this?

Looking at the implementation of clock.tick() and where in that module the FPS value is set (search for _clock->fps =), the documentation seems to be correct that it determines the FPS after 10 calls (which means at the 11th call) to tick(). Let's try with a more isolated example:

import pygame

clock = pygame.time.Clock()
fps = 60

for i in range(20):
print '{}: tick={}, fps={}'.format(i+1, clock.tick(fps), clock.get_fps())


For me, this gives:

1: tick=17, fps=0.0
2: tick=17, fps=0.0
3: tick=18, fps=0.0
4: tick=16, fps=0.0
5: tick=19, fps=0.0
6: tick=17, fps=0.0
7: tick=18, fps=0.0
8: tick=17, fps=0.0
9: tick=17, fps=0.0
10: tick=17, fps=0.0
11: tick=19, fps=57.1428565979
12: tick=17, fps=57.1428565979
13: tick=18, fps=57.1428565979
14: tick=16, fps=57.1428565979
15: tick=19, fps=57.1428565979
16: tick=17, fps=57.1428565979
17: tick=18, fps=57.1428565979
18: tick=16, fps=57.1428565979
19: tick=18, fps=57.1428565979
20: tick=17, fps=57.1428565979


Given the fact that 5 is 10 divided by 2, I'd assume that you call clock.tick() (or clock.tick_busy_loop(), which both call clock_tick_base() internally) either directly or indirectly somewhere else in your game loop, so each iteration has two calls to clock.tick(), resulting in a FPS value already after 5 loop iterations.

This is using Python 2.7.11 on OS X and PyGame from Mercurial (pip install --user hg+http://bitbucket.org/pygame/pygame), which should be revision d61ea8eab at the time of writing, just in case you can't reproduce it on your installation.

What I figured out is you can do 1000 / clock.tick(60) and you would get an approcimation to the fps but it doesn't return 0 for the first 5 frames

try this: you type this in your code (anywhere in the code will work):

while True:
print(clock.getfps())


and BOOM! it just prints the current fps in the console!

• Is this valid pygame code? Sep 1, 2021 at 21:16