javascript - 使用 javascript 测量钛的时间

标签 javascript titanium

我用钛创建了一个应用程序,它创建了 10000 个按钮并将它们添加到窗口中。我的目标是测量整个创建过程的时间,以便我可以将其与其他跨平台解决方案进行比较。

现在我的问题是:所有按钮都完美地绘制出来,但正如标题所说,时间测量相差甚远,用真正的秒表计时大约需要一分钟,但使用 .getTime() 时比较一下,它给了我 0.02 分钟。

function renderButtons() {
    var win = Ti.UI.createWindow({
        backgroundColor: 'B8B8B8',
        exitOnClose: true,
        fullscreen: 'false',
        title: 'Label Demo'
    });

    win.open();
    var count = 0;
    var Pwidth = Ti.Platform.displayCaps.platformWidth;
    var Pheight = Ti.Platform.displayCaps.platformHeight;
    var widthRan = 0;
    var heightRan = 0;
    var left;
    var top;
    var color;
    var time = '0.0';
    var elapsed = '0.0';
    var start = '0.0';

    start = new Date().getTime();
    for (count = 0; count < 10000; count++) {
        left = Math.floor((Math.random()*Pwidth));
        top = Math.floor((Math.random()*Pheight));
        widthRan = Math.floor((Math.random()*100));
        heightRan = Math.floor((Math.random()*100));
        color = getRandomColor();
        var pixel = Ti.UI.createButton({
            center: { x:left, y:top },
            width: widthRan,
            height: heightRan,
            textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER,
            text: 'Back',
            color: 'white',
            backgroundColor: color
        });
        win.add(pixel);
    }

    elapsed = new Date().getTime();
    time = elapsed - start;
    var seconds = time/1000;
    var mins = seconds/60;
    win.close();
    alert(mins.toString());
}

我的时间测量方法是否有偏差,或者这可能是钛的问题?这很奇怪,因为它非常适合我的矩阵乘法。

最佳答案

您正在测量初始化 10000 个按钮对象而不在屏幕上绘制所需的时间。要计算从启动应用程序到用户看到屏幕上所有按钮的确切时间量,您需要使用附加到窗口对象的事件监听器。

尝试这样的事情:

function renderButtons() {
    var win = Ti.UI.createWindow({
        backgroundColor: 'B8B8B8',
        exitOnClose: true,
        fullscreen: 'false',
        title: 'Label Demo'
    });

    var start = new Date().getTime();
    for (var count = 0; count < 1000; count++) {
        win.add( createRandomButton() );
    }
    alert('init: ' + (new Date().getTime() - start));

    win.addEventListener('postlayout', function(){
        alert('postlayout: ' + (new Date().getTime() - start));
    });

    win.open();
}

如果您需要在渲染完成后立即关闭窗口,请在 eventListener 中添加 win.close() 以防止在屏幕上绘制过程中调用它:

win.addEventListener('postlayout', function(){
    alert('postlayout: ' + (new Date().getTime() - start));
    win.close();
});

关于javascript - 使用 javascript 测量钛的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23129765/

相关文章:

javascript - 使用 Office-JS 链接读写

javascript - 具有旋转代理的 Phantomjs/Casperjs?

ios - 在测试工具中运行 Titanium iOS 模块时缺少框架

ios - 应用商店拒绝 iOs 10 上 IPV6 网络支持的应用

css - 在钛中为窗口背景应用渐变颜色的问题

php - 在 php 代码 drupal 7 中使用 drupal_add_js

javascript - 向 Raphaël.js 图像元素添加淡入效果的问题

ios - iOS 上的 Titanium ScrollableView 高度更大

android - 我可以使用钛来构建网络应用程序和移动应用程序吗?

javascript - 如何在单击时将 html 颜色输入值设置为 div 颜色