fps - how to divide count by time function to determine fps

前端 未结 3 1114
傲寒
傲寒 2021-02-14 02:36

I have a counter working that counts every frame. what I want to do is divide this by time to determine the FPS of my program. But I\'m not sure how to perform operations on tim

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-14 03:11

    • Here is a very simple way to print your program's frame rate at each frame (no counter needed) :

      import time
      
      while True:
          start_time = time.time() # start time of the loop
      
          ########################
          # your fancy code here #
          ########################
      
          print("FPS: ", 1.0 / (time.time() - start_time)) # FPS = 1 / time to process loop
      
    • If you want the average frame rate over x seconds, you can do like so (counter needed) :

      import time
      
      start_time = time.time()
      x = 1 # displays the frame rate every 1 second
      counter = 0
      while True:
      
          ########################
          # your fancy code here #
          ########################
      
          counter+=1
          if (time.time() - start_time) > x :
              print("FPS: ", counter / (time.time() - start_time))
              counter = 0
              start_time = time.time()
      

    Hope it helps!

提交回复
热议问题