开发者

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

开发者 https://www.devze.com 2022-12-17 12:48 出处:网络
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

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,


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));
            }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号