Calculating frames per second in a game

后端 未结 19 744
天命终不由人
天命终不由人 2020-12-04 05:10

What\'s a good algorithm for calculating frames per second in a game? I want to show it as a number in the corner of the screen. If I just look at how long it took to render

19条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 05:42

    JavaScript:

    // Set the end and start times
    var start = (new Date).getTime(), end, FPS;
      /* ...
       * the loop/block your want to watch
       * ...
       */
    end = (new Date).getTime();
    // since the times are by millisecond, use 1000 (1000ms = 1s)
    // then multiply the result by (MaxFPS / 1000)
    // FPS = (1000 - (end - start)) * (MaxFPS / 1000)
    FPS = Math.round((1000 - (end - start)) * (60 / 1000));
    

提交回复
热议问题