Last active
January 18, 2017 03:18
-
-
Save zhangyangjing/d6c1b8e8a89c95cbd6b33aff760a95c9 to your computer and use it in GitHub Desktop.
FpsMonitor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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