Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to time how long a function takes?

I have some function I want to time, but I have no idea how to do it. In javascript I can just get the current time in milliseconds, run the rest of my function, get the current time in milliseconds again and alert the difference. Viola, I know how long the function ran.

In ActionScript, it runs everything at once, so my start and end times are the same. How can I measure the span of time a function is taking to process?

Thanks,

like image 900
invertedSpear Avatar asked Dec 05 '25 10:12

invertedSpear


1 Answers

Quick way below. It's better to do a statistical test, eg. run the toMeasure a 100 times between setting time1 and time2 and dividing the result by 100. It might give you a more realistic estimate. Remember that modern computers can do a bit of calculations in under a millisecond.

            private var time1:Number;
            private var time2:Number;

            private function toMeasure():void
            {
                for (var i:int = 0;i<30000;i++)
                {
                    trace (i);
                }
            }

            protected function main():void
            {
                time1= new Date().time;
                toMeasure();
                time2= new Date().time;
                trace ("ms:"+(time2-time1));
            }
like image 186
Robert Bak Avatar answered Dec 07 '25 00:12

Robert Bak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!