dongzhanyan3667 2014-06-15 14:04
浏览 37

为什么在这个例子中NodeJS不是那么快?

I heard a lot about how fast NodeJS is. But my simple test case shows, that Apache with PHP is much faster. The codes look like this:

PHP code

require_once("mysqlconnect.php");
$start = round(microtime(true) * 1000);
$r = mysql_query("SELECT field1 FROM mytable");
$arr = array();
while($s = mysql_fetch_array($r)){
    $arr[] = $s;
}
$obj = json_encode($arr);
$end = round(microtime(true) * 1000);
echo $end-$start;

NodeJS

var mysql = require('mysql'),
start = new Date().getTime(),
connection = mysql.createConnection({
 host     : 'localhost',
 user     : 'root',
 password : 'root',
 database : 'testdb'
}),
json = '',
query = 'SELECT field1 FROM mytable';
connection.connect();
connection.query(query, function(err, results, fields) {
    json = JSON.stringify(results);
    var elapsed = new Date().getTime() - start;
    console.log(elapsed);
    connection.end();
}); 

The table contains 1000 rows and ten tests show that PHP even without any accelerators is at least 3 times faster. Why is that? Does it mean that NodeJS beats PHP only in some special cases?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥16 Qphython 用xlrd读取excel报错
    • ¥15 单片机学习顺序问题!!
    • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
    • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
    • ¥15 相敏解调 matlab
    • ¥15 求lingo代码和思路
    • ¥15 公交车和无人机协同运输
    • ¥15 stm32代码移植没反应
    • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
    • ¥100 连续两帧图像高速减法