Advertisement
Guest User

Timer Class

a guest
Aug 31st, 2012
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. import android.util.Log;
  2.  
  3. public class Timer {
  4.  
  5. int _counter = 0;
  6. String _name;
  7. int _numframes;
  8. boolean started = false;
  9.  
  10. long _starttime;
  11.  
  12. double _cumulative = 0;
  13. double _lastElapsed = 0;
  14. double _lastAverage = 0;
  15. double _lastWorst = 0;
  16.  
  17. public Timer(String name, int framecount){
  18. _name = name;
  19. _numframes = framecount;
  20. }
  21.  
  22. public void start(){
  23. _starttime = System.nanoTime();
  24. started = true;
  25. }
  26.  
  27. public void end(){
  28. if(!started) return;
  29. started = false;
  30.  
  31. long endtime = System.nanoTime();
  32. _lastElapsed = (double)(endtime -_starttime)/ 1.0E9;
  33. _lastWorst = Math.max(_lastWorst, _lastElapsed);
  34. _cumulative += _lastElapsed;
  35.  
  36. if(++_counter >= _numframes){
  37. _lastAverage = _cumulative/_numframes;
  38. Log.d("Timer", _name + ": " +
  39. String.format("%.2f",_lastAverage*1000) +
  40. "ms (worst " +
  41. String.format("%.2f",_lastWorst*1000) +
  42. "ms)"
  43. );
  44. _counter = 0;
  45. _cumulative =0;
  46. _lastWorst = 0;
  47. }
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement