ActionScript - 从代码中分析脚本的速度?

发布于 2024-10-02 16:24:25 字数 632 浏览 1 评论 0原文

我正在尝试使用日期类和跟踪语句从代码中分析我的代码的速度。效果不太好。

package
{   
import flash.display.Sprite;

public class Test extends Sprite
    {
    public function Test()
        {
        var now:Date = new Date();
        var profileSpeedMark:Number = now.getMilliseconds();

        var myArray:Array = new Array();
        for (var i:Number = 0; i < 1000000; i++)
            myArray.push(Math.random());

        var profileSpeedResult:Number = now.getMilliseconds() - profileSpeedMark;
        trace(profileSpeedResult);
        }
    }
}

这里的想法是能够比较不同的函数编码方式并确定哪一种更快。除非我使用的是一台超级慢的计算机,否则我不确定这是否可能,特别是对于非常短的函数。

i'm trying to cave-man-profile the speed of my code from code using the date class and trace statements. it's not working out so well.

package
{   
import flash.display.Sprite;

public class Test extends Sprite
    {
    public function Test()
        {
        var now:Date = new Date();
        var profileSpeedMark:Number = now.getMilliseconds();

        var myArray:Array = new Array();
        for (var i:Number = 0; i < 1000000; i++)
            myArray.push(Math.random());

        var profileSpeedResult:Number = now.getMilliseconds() - profileSpeedMark;
        trace(profileSpeedResult);
        }
    }
}

the idea here is to be able to compare different ways of coding functions and determine which one is faster. unless i'm on a super slow computer i'm not sure if this is possible, especially for really short functions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小清晰的声音 2024-10-09 16:24:25

使用flash.utils包中的getTimer()函数很方便。

var t0:Number = getTimer();
computeSomeThing();
var t1:Number = getTimer();
trace("Time Elapsed: " + String(t1 - t0));

Its convenient to use getTimer() function in the flash.utils package.

var t0:Number = getTimer();
computeSomeThing();
var t1:Number = getTimer();
trace("Time Elapsed: " + String(t1 - t0));
鹤舞 2024-10-09 16:24:25

您在示例代码中执行操作的方式的问题在于 Date 类具有固定值 - 您将测试之前的时间与再次测试之前的时间进行比较。尝试在测试后实例化另一个 Date 对象并从中获取时间:

var before:Date = new Date();
test();
var after:Date = new Date();
var timeTaken:Number = after.time - before.time;

The problem with the way you've done things in your example code is that the Date class has a fixed value -- you're comparing the time before the tests with the time before the tests again. Try instantiating another Date object after your tests and taking the time from that:

var before:Date = new Date();
test();
var after:Date = new Date();
var timeTaken:Number = after.time - before.time;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文