使用 Qt 5.3(预编译安装程序)、Win7 x64,我使用 Qt 的 QWebView 来显示 HTML 页面。 HTML 将执行一个 javascript,它会初始化一个大变量。
slow.html:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function do_load() {
var script = document.createElement('script');
script.src = "slow.js";
document.getElementsByTagName('head')[0].appendChild(script);
}
</script>
</head>
<body>
<input class="btn" type="button" onclick="do_load()" value="load">
</body>
</html>
慢.js:
if (!window.console) {
window.console = {};
}
// union of Chrome, FF, IE, and Safari console methods
var m = [
"log", "info", "warn", "error", "debug", "trace", "dir", "group",
"groupCollapsed", "groupEnd", "time", "timeEnd", "profile", "profileEnd",
"dirxml", "assert", "count", "markTimeline", "timeStamp", "clear"
];
// define undefined methods as noops to prevent errors
for (var i = 0; i < m.length; i++) {
if (!window.console[m[i]]) {
window.console[m[i]] = function() {};
}
}
console.log("begin to init var....");
console.time("init_var");
var HSCARDS= {xxx: yyy........} // Big variable, about 800 KBytes.
console.timeEnd("init_var");
console.log("end init var....");
(完整代码可见:https://gist.github.com/vls/24b8c70fa83b9d88830a
在 Chrome 36 中,变量初始化将耗费大约 10 毫秒,在 IE 9 中也非常快(由于 IE 9 中缺少 console.time,没有准确的时间数据)
但在 Windows 上的 QWebView 中,它将花费大约 10000 ms( Release模式,不附加 IDE)。因为 QWebView 对象在主线程中,这将挂起 MainWindow。 (在装有 Qt 5.3 的 Mac 上,这将花费大约 2000 毫秒,但 不会挂起主窗口)
问题:
如何避免在 Windows 上执行 javascript 时挂起 MainWindow?
为什么 QWebView 中的 javascript 执行这么慢?
最佳答案
为了在 JavaScript 上获得正确的速度,您需要在发布时进行优化编译:
将以下代码添加到您的 QtCreator 项目 (.pro)
QMAKE_CXXFLAGS_RELEASE += -Ofast
就我而言,这解决了问题。
关于Qt 5.3 Webkit QWebView 中的 Javascript 速度太慢,并且在 Windows 上挂起 MainWindow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25299253/