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

前端 未结 3 1099
傲寒
傲寒 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条回答
  •  闹比i
    闹比i (楼主)
    2021-02-14 03:10

    You might want to do something in this taste:

    def program():
      start_time = time.time() #record start time of program
      frame_counter = 0
    
      # random logic 
      for i in range(0, 100):
        for j in range(0, 100):
          # do stuff that renders a new frame
          frame_counter += 1 # count frame
    
      end_time = time.time() #record end time of program
      fps = frame_counter / float(end_time - start_time)
    

    Of course you don't have to wait the end of the program to compute end_time and fps, you can do it every now and then to report the FPS as the program runs. Re-initing start_time after reporting the current FPS estimation could also help with reporting a more precise FPS estimation.

提交回复
热议问题