开发者

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,


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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜