Skip to content

Instantly share code, notes, and snippets.

@zhangyangjing
Last active January 18, 2017 03:18
Show Gist options
  • Save zhangyangjing/d6c1b8e8a89c95cbd6b33aff760a95c9 to your computer and use it in GitHub Desktop.
Save zhangyangjing/d6c1b8e8a89c95cbd6b33aff760a95c9 to your computer and use it in GitHub Desktop.
FpsMonitor
public class FpsMonitor {
private static final int SAMPLE_COUNT = 10;
private ArrayBlockingQueue<Long> mQueue;
private long mLastUpdateTime;
public FpsMonitor() {
mQueue = new ArrayBlockingQueue<>(SAMPLE_COUNT);
mLastUpdateTime = System.currentTimeMillis();
}
public void reset() {
mQueue.clear();
mLastUpdateTime = System.currentTimeMillis();
}
public void updateFrame() {
long currentTime = System.currentTimeMillis();
long elapsedTime = currentTime - mLastUpdateTime;
if (0 == mQueue.remainingCapacity())
mQueue.poll();
mQueue.offer(elapsedTime);
mLastUpdateTime = currentTime;
}
public int getFps() {
int elapsedAverage = (int) DoubleMath.mean(mQueue.iterator());
return 1000 / elapsedAverage;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment