- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我一直在研究一些有趣的编程基准,以了解与其他语言相比 node.js 的性能如何:http://benchmarksgame.alioth.debian.org/u32/compare.php?lang=node&lang2=php
虽然结果主要处理您通常更愿意使用 C 或 Fortran 的变体解决的算法问题,但有一个测试对 V8 非常不利:
pidigits - 比 PHP 慢 52 倍
由于在所有其他测试中,v8 的整体性能都比 PHP 好,我认为代码要么有问题,要么是 V8/Javascript 的特定实现导致其性能如此糟糕。这是什么?
代码 1:V8
// The Computer Language Benchmarks Game
// http://shootout.alioth.debian.org
//
// Contributed by Matthew Wilson
// biginteger derived from Tom Wu's jsbn.js
var compareTo, multiply, divide, addTo, add, intValue, shiftLeft, nbv;
function main($n) {
var $i=1, $s="", $d, neg10=nbv(-10), three=nbv(3), ten=nbv(10), g = 1, $g,
digits=Array(10), $z0=nbv(1), $z1=nbv(0), $z2=nbv(1), negdigits=Array(10),
k = 0, $k, l = 2, $l, a;
for(var i=0; i<10; ++i) { negdigits[i] = multiply(digits[i] = nbv(i),neg10) }
do {
while ( compareTo($z0,$z2) > 0
|| ($d = intValue(divide(add(multiply($z0,three),$z1),$z2))) !=
intValue(divide(add(shiftLeft($z0,2),$z1),$z2))
) {
$z1 = multiply($z1,$g = nbv(g+=2));
$z2 = multiply($z2,$g);
addTo($z1, multiply($z0,$l = nbv(l+=4)), $z1);
$z0 = multiply($z0,$k = nbv(++k));
}
$z0 = multiply($z0,ten);
$z1 = multiply($z1,ten);
addTo($z1, multiply($z2,negdigits[$d]), $z1);
$s += $d;
if ($i % 10 == 0) { print($s+"\t:"+$i); $s="" }
} while (++$i <= $n)
if (($i = $n % 10) != 0) { $s += Array(11-$i).join(' ') }
if ($s.length > 0) { print($s+"\t:"+$n) }
}
var functions;
load('/home/dunham/shootout/bench/Include/javascript/biginteger.js');
compareTo=functions[0];
multiply=functions[1];
divide=functions[2];
addTo=functions[3];
add=functions[4];
nbv=functions[5];
shiftLeft=functions[6];
intValue=functions[7];
main.call(this, 1*arguments[0]*1)
代码 2:PHP
<?php /* The Great Computer Language Shootout
http://shootout.alioth.debian.org/
contributed by Isaac Gouy
php -q pidigits.php 27
*/
class Transformation {
var $q, $r, $s, $t, $k;
function Transformation($q, $r, $s, $t){
$this->q = $q;
$this->r = $r;
$this->s = $s;
$this->t = $t;
}
function Unity(){
return new Transformation("1", "0", "0", "1");
}
function Zero(){
return new Transformation("0", "0", "0", "0");
}
function Compose($a){
$qq = bcmul($this->q, $a->q);
$qrrt = bcadd(bcmul($this->q, $a->r), bcmul($this->r, $a->t));
$sqts = bcadd(bcmul($this->s, $a->q), bcmul($this->t, $a->s));
$srtt = bcadd(bcmul($this->s, $a->r), bcmul($this->t, $a->t));
return new Transformation($qq, $qrrt, $sqts, $srtt);
}
function Extract($j){
$bigj = strval($j);
$qjr = bcadd(bcmul($this->q, $bigj), $this->r);
$sjt = bcadd(bcmul($this->s, $bigj), $this->t);
$d = bcdiv($qjr, $sjt);
return floor($d);
}
function Next(){
$this->k = $this->k + 1;
$this->q = strval($this->k);
$this->r = strval(4*$this->k + 2);
$this->s = "0";
$this->t = strval(2*$this->k + 1);
return $this;
}
}
class PiDigitStream {
var $z, $x, $inverse;
function PiDigitStream(){
$this->z = Transformation::Unity();
$this->x = Transformation::Zero();
$this->inverse = Transformation::Zero();
}
function Produce($j){
$i = $this->inverse;
$i->q = "10";
$i->r = strval(-10*$j);
$i->s = "0";
$i->t = "1";
return $i->Compose($this->z);
}
function Consume($a){
return $this->z ->Compose($a);
}
function Digit(){
return $this->z ->Extract(3);
}
function IsSafe($j){
return $j == ($this->z ->Extract(4));
}
function Next(){
$y = $this->Digit();
if ($this->IsSafe($y)){
$this->z = $this->Produce($y);
return $y;
} else {
$this->z = $this->Consume($this->x ->Next());
return $this->Next();
}
}
}
$n = $argv[1];
$i = 0;
$length = 10;
$pidigit = new PiDigitStream;
while ($n > 0){
if ($n < $length){
for ($j=0; $j<$n; $j++) printf("%d",$pidigit->Next());
for ($j=$n; $j<$length; $j++) print " ";
$i += $n;
} else {
for ($j=0; $j<$length; $j++) printf("%d",$pidigit->Next());
$i += $length;
}
print "\t:$i\n";
$n -= $length;
}
?>
最佳答案
PHP 正在使用 BC Math library 高度优化 GMP library它的计算是用 C 语言编写的(在某些地方是汇编语言),其中 V8 版本使用一个用 JavaScript 编写的大整数类(它说“基于”Tom Wu's jsbn.js)。相比 V8 和 PHP,比较 V8 和 C 大整数性能的基准可能更准确。
问题中的 PHP 代码是使用 BC Math 库的 PHP 条目的不同版本,实际上比 V8 慢(感谢 iguy)。 BC 库也是用 C 编写的,但它使用以 10 为基数的数字(它是 GNU 版本的 dc
和 bc
使用的库的 PHP 包装器)并且不是不像 GMP 那样优化。
关于javascript - 为什么这个 V8/Javascript 代码表现如此糟糕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7025286/
我正在用 C++ 开发一个程序,我必须实现一个 cron。由于不同的原因,这个 cron 应该每小时和每 24 小时执行一次。我的第一个想法是创建一个独立的 pthread 并在每次 1h 内休眠。这
我需要向同一场景几何添加多个体素(立方体等于),但每个体素具有不同的纹理。 我的体素超过 500 个,导致性能出现严重错误。 这是我的代码: texture = crearTextura(voxel.
对于 MySQL 数据库,我有 2 个场景,我不确定该选择哪一个,并且对于一些表我也遇到了同样的困境。 我正在制作一个仅供成员(member)访问的网络应用程序。每个成员都有自己的交易、费用和“列表”
我想知道一个简单的事情: 当设置一个被所有 child 继承的样式时,是否建议最具体? Structure: html > body > parent_content > wrapper > p 我想
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
这些天我正在阅读有关 JPA 的内容。我了解到可以在 JPQL 中使用 explicit 或 implicit JOIN。 显式加入 em.createQuery(“SELECT b.title, p
我有一种情况需要连接几个字符串以形成一个类的 id。基本上,我只是在列表中循环以获取对象的 ToString 值,然后将它们连接起来。 foreach (MyObject o in myList)
我正在检查我的游戏在拖尾效果下的性能会降低多少。但我注意到每秒的操作次数更多了。这怎么可能? 这是怎么回事... context.fillRect(0, 0, 500, 500); // cl
如果我可以选择使用全局变量或传递变量,哪个选项在速度和内存使用方面更好? // global variable function func(){ global $var; echo $var;
我有一个类似这样的表“tbl”:ID bigint(20) - 主键,自增字段1字段2字段3 该表有 60 万多行。 查询:SELECT * from tbl ORDER by ID LIMIT 60
谁能告诉我,我如何比较 TSP 最优和启发式算法?我已经实现了 TSP,但不知道如何比较它们。事实上,我怎样才能找到 TSP 的最优成本?有什么方法或猜测吗? 谢谢 最佳答案 用众所周知的基准实例检查
我有一个 NSTextStorage里面有长文本(比如一本书有 500 页,当前字体在设备上超过 9000 页)。我以这种方式为 textcontainer 分发此文本: let textStorag
我有一个根据邮政编码搜索项目的应用程序。 在搜索邮政编码时,我返回了来自该城市/社区的所有产品(通过解析邮政编码完成)。 我现在需要根据与原始邮政编码的距离对这些产品进行分类。 我将纬度/经度存储在数
我有许多进程(大约100到1000个进程),每个进程都必须向其他进程(例如大约10个)发送一些数据。 (通常,但不一定总是这样,如果A发送给B,B也发送给A。)每个进程都知道必须从哪个进程接收多少数据
我知道无状态组件使用起来更舒服(在特定场景下),但是既然你不能使用shouldComponentUpdate,这是否意味着组件将在每次props更改时重新渲染?我的问题是,使用带有智能 shouldC
我正在研究 Google Pagespeed 的加速页面加载时间指南列表。其中之一是缩小 CSS 和 JS 文件。 由于这些文件经常更改,我正在考虑使用 PHP 脚本根据请求(来自浏览器)即时缩小此脚
我正在尝试从下表构建 SQL 查询(示例): Example of table with name "performances" 这是带有运动表现的表格。我想从这个表中选择每个学科和一组一个或多个类别
假设我们有一个字符串 var "sA",我想检查字符串 "123"是否在 sA 的末尾。 什么更好,为什么: if(sA.length() > 2) sA.substr(sA.length()-3)
关于受这篇文章启发的可参数化查询 LINQ group by property as a parameter我获得了一个很好的参数化查询,但在性能上有一个缺点。 public static void
| 和| 之间有什么主要区别吗?和 + 从长远来看会影响代码的性能吗?或者都是 O(1)?我正在使用的代码是这样的: uint64_t dostuff(uint64_t a,uint64_t b){
我是一名优秀的程序员,十分优秀!