Node.js與PHP、Python的字元處理效能對比,node.jspython

來源:互聯網
上載者:User

Node.js與PHP、Python的字元處理效能對比,node.jspython

測試案例分為用函數和類來進行一個大字串的字元逐一讀取。

測試代碼

Node.js

函數

var fs = require("fs");var content = fs.readFileSync("page.html", { encoding: "utf-8"});function chars(content){ var length = content.length; var pos = 0; while(pos ++ < length){  var chr = content[pos - 1]; }}var start = Date.now();chars(content);var end = Date.now();console.log(end - start);

var fs = require("fs");var content = fs.readFileSync("page.html", { encoding: "utf-8"});var Chars = function(str){ this.str = str; this.length = str.length this.pos = 0;}Chars.prototype.run = function(){ while(this.pos ++ < this.length){  var chr = this.str[this.pos - 1]; }}var start = Date.now();var instance = new Chars(content);instance.run();var end = Date.now();console.log(end - start);

PHP

函數

<?phpfunction chars($content){ $length = strlen($content); $pos = 0; while ($pos ++ < $length) {  $char = $content{$pos - 1}; }}$content = file_get_contents("page.html");$start = microtime(true);chars($content);$end = microtime(true);echo ($end - $start) . "\n";?>

<?phpclass Chars{ public function __construct($str){  $this->str = $str;  $this->length = strlen($str);  $this->pos = 0; } public function run(){  while($this->pos++ < $this->length){   $char = $this->str{$this->pos - 1};  } }}$content = file_get_contents("page.html");$start = microtime(true);$instance = new Chars($content);$instance->run();$end = microtime(true);echo ($end - $start) . "\n";?>

Python

函數

import codecsimport timedef chars(content): length = len(content) pos = 0 while(pos < length):  char = content[pos]  pos = pos + 1f = codecs.open('page.html', encoding='utf-8')content = f.read()start = time.time()chars(content)end = time.time();print end - start

import codecsimport timeclass Chars():  def __init__(self, str):   self.str = str  self.length = len(str)  self.pos = 0 def run(self):  while(self.pos < self.length):   char = self.str[self.pos]   self.pos = self.pos + 1f = codecs.open('page.html', encoding='utf-8')content = f.read()start = time.time()instance = Chars(content)instance.run()end = time.time();print (end - start)

其中 page.html 檔案內容為一個長度為 的文本。

測試結果

語言 函數 類Node.js 0.022s 0.026sPHP 0.35s 1.02sPython 0.58s 1.50s


開發一個網站用php還是nodejs?

我不覺得node.js有什麼不適合新手的,畢竟你從0開始學PHP一樣要對網頁編程有個學習的過程,所以從上手程度來說兩者幾乎等價。一樣要瞭解MVC,資料庫連接等等。
直接比較一下上手時的優劣,你可以自己衡量一下
Node.js
優點:js文法,對會用js的人來說看懂代碼不難。

簡化了設定管理員的過程,內建一個web伺服器,不像php要安裝配置apache
缺點:相比PHP學習資料較少
PHP
優點:學習資料多,架構多,工具包多
缺點:對於文法有個熟悉過程
我覺得這應該是你搭的第一個網站,語言的重要性其實不那麼大,優缺點可能在你學習的過程中也不會體現,而且都是一些外在外因。當然你也可以這麼考慮,現在搞PHP的一抓一大把,而且主要以輕量級公司用的居多。node被很多大公司所推崇,越早接觸機會機會越多。
 
php 字串處理

代碼如下:

<?php
$str = "my name's haha";

echo addslashes($str);
?>

輸出結果:

my name\'s haha
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.