Timer.java 657 B

12345678910111213141516171819202122232425
  1. package me.hammerle.snuviengine.api;
  2. public class Timer {
  3. private static final int SIZE = 32;
  4. private int index = -1;
  5. private final long[] times = new long[SIZE];
  6. private long sum = 0;
  7. private double callsPerSecond = 0;
  8. private long lastTime = System.nanoTime();
  9. public void update() {
  10. index = (index + 1) % SIZE;
  11. long time = System.nanoTime();
  12. sum -= times[index];
  13. times[index] = time - lastTime;
  14. sum += times[index];
  15. lastTime = time;
  16. callsPerSecond = (1_000_000_000.0 * SIZE) / sum;
  17. }
  18. public double getCallPerSecond() {
  19. return callsPerSecond;
  20. }
  21. }