请在 Firefox 上运行此测试。
http://jsperf.com/static-arithmetic
你会如何解释结果?
这
b = a + 5*5;
b = a + 6/2;
b = a + 7+1;
执行速度比
b = a + 25;
b = a + 3;
b = a + 8;
为什么?
请在 Firefox 上运行此测试。
http://jsperf.com/static-arithmetic
你会如何解释结果?
这
b = a + 5*5;
b = a + 6/2;
b = a + 7+1;
执行速度比
b = a + 25;
b = a + 3;
b = a + 8;
为什么?
在 Firefox 中,它看起来与浮点数学与整数数学有关,其中浮点数要快得多。当我添加一些浮点数学时,您可以看到不同之处:http: //jsperf.com/static-arithmetic/14。
这要快得多:
b = a + 26.01;
b = a + 3.1;
b = a + 8.2;
比这个:
b = a + 25;
b = a + 3;
b = a + 8;
我所能猜测的是,Firefox 有一些浮点优化不适用于整数数学,或者当涉及浮点数时,代码不知何故采取了不同的路径。
因此,将此信息推断为您的原始答案,+ 5*5
必须使用更快的浮动路径,而+ 25
不是。有关更多详细信息,请参阅引用的 jsPerf。
一旦你让一切都浮起来,这个+ (5.1 * 5.1)
选项就会比+ 26.01
我们预期的要慢。
首先,你的测试有一点缺陷。
您应该比较以下内容:
b = a + 8 - 2;
对比b = a + 6
b = a + 8 + 2;
对比b = a + 10
b = a + 8 / 2;
对比b = a + 4
b = a + 8 * 2;
对比b = a + 16
您会注意到一些有趣的事情:只有具有+
或-
在第二对术语中的问题较慢(除法和乘法都可以)。加法/减法和乘法/除法的实现之间必须有明显的区别。确实有:
所以让我们看看加法和乘法(jsparse.cpp):
JSParseNode *
Parser::addExpr()
{
JSParseNode *pn = mulExpr();
while (pn &&
(tokenStream.matchToken(TOK_PLUS) ||
tokenStream.matchToken(TOK_MINUS))) {
TokenKind tt = tokenStream.currentToken().type;
JSOp op = (tt == TOK_PLUS) ? JSOP_ADD : JSOP_SUB;
pn = JSParseNode::newBinaryOrAppend(tt, op, pn, mulExpr(), tc);
}
return pn;
}
JSParseNode *
Parser::mulExpr()
{
JSParseNode *pn = unaryExpr();
while (pn && (tokenStream.matchToken(TOK_STAR) || tokenStream.matchToken(TOK_DIVOP))) {
TokenKind tt = tokenStream.currentToken().type;
JSOp op = tokenStream.currentToken().t_op;
pn = JSParseNode::newBinaryOrAppend(tt, op, pn, unaryExpr(), tc);
}
return pn;
}
但是,正如我们所知,这里并没有太大的区别。两者都以类似的方式实现并且都调用newBinaryOrAppend()
.. 那么这个函数到底是什么?
(剧透:它的同名可能会背叛为什么加法/减法的成本更高。再次查看jsparse.cpp)
JSParseNode *
JSParseNode::newBinaryOrAppend(TokenKind tt, JSOp op, JSParseNode *left, JSParseNode *right,
JSTreeContext *tc)
{
JSParseNode *pn, *pn1, *pn2;
if (!left || !right)
return NULL;
/*
* Flatten a left-associative (left-heavy) tree of a given operator into
* a list, to reduce js_FoldConstants and js_EmitTree recursion.
*/
if (PN_TYPE(left) == tt &&
PN_OP(left) == op &&
(js_CodeSpec[op].format & JOF_LEFTASSOC)) {
if (left->pn_arity != PN_LIST) {
pn1 = left->pn_left, pn2 = left->pn_right;
left->pn_arity = PN_LIST;
left->pn_parens = false;
left->initList(pn1);
left->append(pn2);
if (tt == TOK_PLUS) {
if (pn1->pn_type == TOK_STRING)
left->pn_xflags |= PNX_STRCAT;
else if (pn1->pn_type != TOK_NUMBER)
left->pn_xflags |= PNX_CANTFOLD;
if (pn2->pn_type == TOK_STRING)
left->pn_xflags |= PNX_STRCAT;
else if (pn2->pn_type != TOK_NUMBER)
left->pn_xflags |= PNX_CANTFOLD;
}
}
left->append(right);
left->pn_pos.end = right->pn_pos.end;
if (tt == TOK_PLUS) {
if (right->pn_type == TOK_STRING)
left->pn_xflags |= PNX_STRCAT;
else if (right->pn_type != TOK_NUMBER)
left->pn_xflags |= PNX_CANTFOLD;
}
return left;
}
/*
* Fold constant addition immediately, to conserve node space and, what's
* more, so js_FoldConstants never sees mixed addition and concatenation
* operations with more than one leading non-string operand in a PN_LIST
* generated for expressions such as 1 + 2 + "pt" (which should evaluate
* to "3pt", not "12pt").
*/
if (tt == TOK_PLUS &&
left->pn_type == TOK_NUMBER &&
right->pn_type == TOK_NUMBER) {
left->pn_dval += right->pn_dval;
left->pn_pos.end = right->pn_pos.end;
RecycleTree(right, tc);
return left;
}
pn = NewOrRecycledNode(tc);
if (!pn)
return NULL;
pn->init(tt, op, PN_BINARY);
pn->pn_pos.begin = left->pn_pos.begin;
pn->pn_pos.end = right->pn_pos.end;
pn->pn_left = left;
pn->pn_right = right;
return (BinaryNode *)pn;
}
鉴于上述情况,特别是恒定折叠:
if (tt == TOK_PLUS &&
left->pn_type == TOK_NUMBER &&
right->pn_type == TOK_NUMBER) {
left->pn_dval += right->pn_dval;
left->pn_pos.end = right->pn_pos.end;
RecycleTree(right, tc);
return left;
}
并考虑到在制定问题时
b = Number(a) + 7 + 2;
对比b = Number(a) + 9;
...问题完全消失了(尽管由于我们正在调用静态方法,它显然要慢得多),我很想相信任何一个常量折叠都被破坏了(这似乎不太可能,因为括号内的折叠似乎工作正常),Spidermonkey 没有将数字文字(或数字表达式,即 ie b = a + ( 7 + 2 )
)分类为TOK_NUMBER
(至少在第一个解析级别),这也不太可能,或者我们递归地下降到某个地方太深。
我没有使用过 Spidermonkey 代码库,但我的 Spidey 感觉告诉我我们在某个地方迷路了,我感觉它在RecycleTree()
.
Firefox 版本 4-8 有两种不同的 JIT:Tracemonkey (tracejit) 和 JaegerMonkey (methodjit)。TraceMonkey 在简单的数字代码上要好得多;JaegerMonkey 在各种分支代码上要好得多。
有一个启发式方法用于决定使用哪个 JIT。它着眼于一堆因素,其中大部分因素在这里无关紧要,但对于这个测试用例来说重要的是循环体中的算术操作越多,使用 TraceMonkey 的可能性就越大。
您可以通过更改 和 的值来测试这一点,javascript.options.tracejit.content
以javascript.options.methodjit.content
强制代码在一个或另一个 JIT 下运行,然后查看它如何影响性能。
看起来恒定折叠并没有在使测试用例行为相同方面节省时间,因为 Spidermonkey 不能恒定折叠a + 7 + 1 = (a + 7) + 1
,a + 8
因为它不知道是什么a
(例如,"" + 7 + 1 == "71"
而"" + 8 == "8"
)。如果你这样写,a + (7 + 1)
那么你会突然让另一个 JIT 在这段代码上运行。
所有这些都证明了从微基准推断到实际代码的危险。;)
哦,Firefox 9 只有一个 JIT(JaegerMonkey 基于 Brian Hackett 的类型推断工作进行了优化,使其在此类算术代码上也很快)。
在 Windows XP 上测试 Firefox 3.6.23 测试 Ops/sec 分配算法
b = a + 5*5;
b = a + 6/2;
b = a + 7+1;
67,346,939 ±0.83%11% slower assign plain
b = a + 25;
b = a + 3;
b = a + 8;
75,530,913 ±0.51%fastest
在 Chrome 中并非如此。
为了我:
b = a + 5*5;
b = a + 6/2;
b = a + 7+1;
结果:267,527,019,±0.10%,慢 7%
和
b = a + 25;
b = a + 3;
b = a + 8;
结果:288,678,771,±0.06%,最快
所以,不是真的......不知道为什么它会在 Firefox 上这样做。
(在 Windows Server 2008 R2 / 7 x64 上的 Chrome 14.0.835.202 x86 中测试)