nodejs的效率让人吃惊
Tofloor
poster avatar
billy123456
deepin
2018-06-19 03:31
Author
以前以为nodejs挺快的,然后今天和java的jjs比较一下
源文件不同是因为jjs是es5,而node是es6

[billy@billy-pc cmp]$ time jjs test.jjs
468888890

real    0m3.647s
user    0m5.679s
sys    0m0.506s
[billy@billy-pc cmp]$ time node test.js
468888890

real    0m12.745s
user    0m13.136s
sys    0m0.739s


Reply Favorite View the author
All Replies
2 / 2
To page
avatar
duanyao
deepin
2018-06-21 03:53
#21
https://bbs.deepin.org/post/158462
按照你的说法改了程序
$ time jjs 1.js

jjs 的时间还是不正常,你在代码里打印一下时间,看看 real  和 user 哪个错了?
然后把循环次数增加10倍,看看 jjs 的 CPU 占用是否超过一个线程(按说不会)。
Reply View the author
avatar
billy123456
deepin
2018-06-21 05:40
#22
https://bbs.deepin.org/post/158462
jjs 的时间还是不正常,你在代码里打印一下时间,看看 real  和 user 哪个错了?
然后把循环次数增加10倍 ...

加入了打印时间代码
[billy@billy-pc test]$ time jjs 1.js
1.308s

real    0m2.034s
user    0m4.148s
sys     0m0.113s
Reply View the author
avatar
duanyao
deepin
2018-06-21 18:07
#23
https://bbs.deepin.org/post/158462
加入了打印时间代码
$ time jjs 1.js
1.308s

循环次数增加10倍看看。
Reply View the author
avatar
billy123456
deepin
2018-06-21 21:00
#24
https://bbs.deepin.org/post/158462
循环次数增加10倍看看。

[billy@billy-pc test]$ time jjs 1.js
10.582s

real    0m11.343s
user    0m14.030s
sys     0m0.168s
Reply View the author
avatar
duanyao
deepin
2018-06-21 21:25
#25
https://bbs.deepin.org/post/158462
$ time jjs 1.js
10.582s

运行期间jjs的CPU占用率呢?
Reply View the author
avatar
billy123456
deepin
2018-06-21 21:33
#26
https://bbs.deepin.org/post/158462
运行期间jjs的CPU占用率呢?

JJS 100%
NODE 185
Reply View the author
avatar
duanyao
deepin
2018-06-21 21:46
#27

那么 time 程序就不对了,只有利用了多核的程序会出现 user 大于 real 的情况。
node 的 CPU 占用也不对,它是单线程的。
Reply View the author
avatar
jiuxian
deepin
2018-06-21 23:01
#28
https://bbs.deepin.org/post/158462
这个程序的 sum 变量超出了 32 位整数的表示范围,js 中是用 double 表示的,double 运算看来是 v8 和 nas ...

这实验基本可以证实我的猜测,nodejs的运行库恐怕都是用脚本实现的,jjs的库很可能是class。
Reply View the author
avatar
billy123456
deepin
2018-06-22 06:05
#29
https://bbs.deepin.org/post/158462
那么 time 程序就不对了,只有利用了多核的程序会出现 user 大于 real 的情况。
node 的 CPU 占用也不对 ...

所以为什么会这样?
Reply View the author
avatar
duanyao
deepin
2018-06-22 17:24
#30
https://bbs.deepin.org/post/158462
这实验基本可以证实我的猜测,nodejs的运行库恐怕都是用脚本实现的,jjs的库很可能是class。 ...

并不是,v8和nashorn + JVM都是 JIT 编译器,代码都会编译成机器码再执行,只不过优化方式和程度有差异。有兴趣可以看看这个:
*引擎的资料链接
http://hllvm.group.iteye.com/group/topic/37596
Reply View the author
avatar
duanyao
deepin
2018-06-22 18:07
#31
https://bbs.deepin.org/post/158462
所以为什么会这样?

不知道,我这里 jjs 的 user 只是比 real 略大,或许可以解释为 JIT 编译器、GC是在不同线程并行执行的。
CPU占用根据 top 都是 100%。
Reply View the author
2 / 2
To page