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
1 / 2
To page
avatar
billy123456
deepin
2018-06-19 03:42
#1
这里是java语言
[billy@billy-pc cmp]$ time java test
468888890

real        0m1.651s
user        0m1.385s
sys        0m0.436s
Reply View the author
avatar
jiuxian
deepin
2018-06-19 03:49
#2
脚本跟字节码的差距,不用吃惊。
Reply View the author
avatar
billy123456
deepin
2018-06-19 03:52
#3
https://bbs.deepin.org/post/158462
脚本跟字节码的差距,不用吃惊。

jjs*好吧
Reply View the author
avatar
jiuxian
deepin
2018-06-19 06:01
#4

你想表达什么?
Reply View the author
avatar
billy123456
deepin
2018-06-19 06:33
#5

我的意思是,jjs也是脚本语言
Reply View the author
avatar
billy123456
deepin
2018-06-19 06:33
#6

所以脚本不脚本不是问题
Reply View the author
avatar
jiuxian
deepin
2018-06-19 20:10
#7
https://bbs.deepin.org/post/158462
我的意思是,jjs也是脚本语言

这说明nodejs里的运行库是脚本,jjs的运行库是class(猜的)
Reply View the author
Comments
billy123456
2018-06-19 21:42
貌似是的
avatar
billy123456
deepin
2018-06-20 04:59
#8
https://bbs.deepin.org/post/158462
这说明nodejs里的运行库是脚本,jjs的运行库是class(猜的)

但是这样解释貌似有问题,纯数字计算,nodejs依然慢
[billy@billy-pc cmpjs]$ bash run.sh
Node.js
Run node test.js
4294967295

real    1m57.582s
user    3m21.116s
sys     0m4.786s


Nashorn
Run jjs test.js
4294967295

real    0m29.020s
user    0m31.338s
sys     0m0.057s
Reply View the author
avatar
billy123456
deepin
2018-06-20 05:03
#9
这次改成通用的脚本了,除了console.log和print没有用到其他库,贴上新的代码if(typeof console!="undefined")
a=console.log;
else
a=print;
var c=0;
var hanoi=function(n,from,ass,to){
if(n>0){
hanoi(n-1,from,to,ass);
move(n,from,to);
hanoi(n-1,ass,from,to);
}
}
var move=function(n,from,to){
c++;
}
hanoi(32,"A","B","C");
a(c);

Reply View the author
avatar
billy123456
deepin
2018-06-20 05:04
#10
https://bbs.deepin.org/post/158462
这次改成通用的脚本了,除了console.log和print没有用到其他库,贴上新的代码if(typeof console!="undefine ...

是在百度上的代码改了一下出来的
Reply View the author
avatar
jiuxian
deepin
2018-06-20 18:37
#11
https://bbs.deepin.org/post/158462
是在百度上的代码改了一下出来的

试试纯数值运算,比如for循环从1加到100000,不要打印,一打印你就不知道里面是啥了。
Reply View the author
Comments
billy123456
2018-06-20 21:13
而且你这样计算量太小了
billy123456
2018-06-20 21:04
汉诺塔问题不是纯数值运算?
avatar
billy123456
deepin
2018-06-20 21:02
#12
https://bbs.deepin.org/post/158462
试试纯数值运算,比如for循环从1加到100000,不要打印,一打印你就不知道里面是啥了。 ...

这个难道不是纯数值运算吗?而且我就打印了一行东西,对性能有很大影响吗?
Reply View the author
avatar
billy123456
deepin
2018-06-20 21:23
#13
这里的a.js是个空文件,貌似node就是启动快,但是启动之后效率低下
[billy@billy-pc ~]$ time jjs a.js

real    0m0.699s
user    0m2.192s
sys     0m0.084s
[billy@billy-pc ~]$ time node a.js

real    0m0.066s
user    0m0.056s
sys     0m0.010s
Reply View the author
avatar
duanyao
deepin
2018-06-20 23:36
#14
总的来说 nashorn 远不如 v8 快,但具体成绩非常依赖于你具体在测什么:
http://blog.nidi.guru/tech/javas ... ascript-benchmarks/

顺便一提,nashorn 将要被 GraalVM 取代,后者应该会快一些。
https://www.infoq.com/news/2018/06/deprecate-nashorn
Reply View the author
Comments
billy123456
2018-06-21 02:19
就是据说nodejs用了v8技术,我才对它的效率感到不满啊
billy123456
2018-06-21 02:14
第一个是在测字符串拼接
avatar
duanyao
deepin
2018-06-20 23:49
#15
https://bbs.deepin.org/post/158462
这次改成通用的脚本了,除了console.log和print没有用到其他库,贴上新的代码if(typeof console!="undefine ...

这个程序的计算只有 "c++" 这一句,绝大部分开销在函数调用上,测试的是函数调用性能。
Reply View the author
avatar
billy123456
deepin
2018-06-21 02:15
#16
https://bbs.deepin.org/post/158462
总的来说 nashorn 远不如 v8 快,但具体成绩非常依赖于你具体在测什么:
http://blog.nidi.guru/tech/javas ...

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

real        0m2.064s
user        0m4.212s
sys        0m0.241s
[billy@billy-pc test]$ time node 1.js

real        0m3.625s
user        0m6.217s
sys        0m0.258s
[billy@billy-pc test]$ cat 1.js
for(i=0,sum=0;i<=100000000;i++)
        sum+=i;
[billy@billy-pc test]$
Reply View the author
avatar
duanyao
deepin
2018-06-21 03:23
#17
本帖最后由 duanyao 于 2018-6-20 19:41 编辑
https://bbs.deepin.org/post/158462
$ time jjs 1.js

real        0m2.064s

这个程序的 sum 变量超出了 32 位整数的表示范围,js 中是用 double 表示的,double 运算看来是 v8 和 nashorn 优化的盲点,等效的 C 程序运行时间是 0.11 秒左右:


  1. #include
  2. int main() {
  3.   double sum = 0;
  4.   for(int i=0; i <= 100000000; i++)
  5.     sum += i;
  6.   printf("%f\n", sum);
  7.   return 0;
  8. }
Copy the Code


如果强制在 32 位整数范围内运行,结果就会大大不同:


  1. var sum = 0;
  2. for(var i = 0; i <= 100000000; i++)
  3.     sum = (sum + i) | 0;
Copy the Code


nodejs:0.19秒,jjs:3.22秒。可见在 32 位整数运算方面 nodejs 很快(与原生代码差距还很大,C语言32 位整数版是0.029秒),而 nashorn 似乎没有做特别的优化。

另外,你这个 time 结果 user 远比 real 大是怎么回事儿?
Reply View the author
avatar
billy123456
deepin
2018-06-21 03:36
#18
https://bbs.deepin.org/post/158462
这个程序的 sum 变量超出了 32 位整数的表示范围,js 中是用 double 表示的,double 运算看来是 v8 和 nas ...

我哪知道user为什么比real大这么多
Reply View the author
avatar
duanyao
deepin
2018-06-21 03:39
#19
https://bbs.deepin.org/post/158462
我哪知道user为什么比real大这么多

肯定有问题。你测一下我写的C语言版。
Reply View the author
avatar
billy123456
deepin
2018-06-21 03:46
#20
https://bbs.deepin.org/post/158462
这个程序的 sum 变量超出了 32 位整数的表示范围,js 中是用 double 表示的,double 运算看来是 v8 和 nas ...

按照你的说法改了程序
[billy@billy-pc test]$ time jjs 1.js

real    0m2.602s
user    0m5.333s
sys     0m0.166s
[billy@billy-pc test]$ time node 1.js

real    0m0.376s
user    0m0.367s
sys     0m0.007s
[billy@billy-pc test]$
Reply View the author
1 / 2
To page