# FPS, Memory Usage, Time Elapse - Code optimization

**URL:** <https://forum.kirupa.com/t/fps-memory-usage-time-elapse-code-optimization/330399>\
**Category:** flash\
**Created:** [January 15, 2013, 4:39am UTC](https://forum.kirupa.com/t/fps-memory-usage-time-elapse-code-optimization/330399 "2013-01-15T04:39:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![BlueWallet](https://avatars.discourse-cdn.com/v4/letter/b/9e8a1a/32.png) [@BlueWallet](https://forum.kirupa.com/u/BlueWallet)\
**Post date:** [January 15, 2013, 4:39am UTC](https://forum.kirupa.com/t/fps-memory-usage-time-elapse-code-optimization/330399/1 "2013-01-15T04:39:12Z")

</div>

Greetings all,  
I am making a FPS counter, Memory Usage and Time elapse tool for myself to see what my SWF’s are doing.

Anyone see where I can improve it? Any helpful advice is appreciated.

```auto

package {

	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.system.System;
	import flash.utils.getTimer;

	public class SWFHelper extends MovieClip {

		private var mem:String;
        	private var lastCount:uint = getTimer();
        	private var ticker:uint = 0;
		private var fps:Number;
		private var timePlayed:uint = 0;
		private var totTime:uint = 0;
		private var currFrame:uint = 0;
		private var totFrames:uint = 0;
		
		public function SWFHelper() {
			if (stage) {
				init();
			} else {
				this.addEventListener(Event.ADDED_TO_STAGE, init);
			}
		}

		private function init(e:Event = null):void {
			this.removeEventListener(Event.ADDED_TO_STAGE, init);
			totFrames = MovieClip(root).totalFrames;
			totTime = totFrames / 24; //24 FPS
			this.addEventListener(Event.ENTER_FRAME, update);
		}
		
		private function update(e:Event):void { 
			ticker++;
            		var nowCount:uint = getTimer();
           		var calcCount:uint = nowCount - lastCount;
            		if (calcCount >= 1000) {
                		fps = ticker / calcCount * 1000;
				mem = Number(System.totalMemory/1024/1024).toFixed(2) + " MB";
				ticker = 0;
                		lastCount = nowCount;
            		}
			timePlayed = Math.floor(nowCount/1000);
			currFrame = MovieClip(root).currentFrame;
			if (currFrame == totFrames) {
					this.removeEventListener(Event.ENTER_FRAME, update);
			}
			setElements();
		}
		
		private function setElements():void {		 	
				fPS.text = "FPS: " + fps.toFixed(1);
				memory.text = "MEM: " + mem;
			 	timeCount.text = "TIME PLAYED: " + timePlayed + " of " + totTime;
				curFrame.text = "FRAMES PLAYED: " + currFrame + " / " + totFrames;
		}
	}
}

```
