我用钛创建了一个应用程序,它创建了 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/