国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Node.js與PHP、Python的字符處理性能對比

2019-11-20 14:22:12
字體:
來源:轉載
供稿:網友

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

測試代碼

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 嘉祥县| 德阳市| 庆元县| 南江县| 临邑县| 辰溪县| 龙陵县| 无为县| 裕民县| 若羌县| 乐清市| 西充县| 礼泉县| 延川县| 错那县| 龙山县| 额济纳旗| 张北县| 五华县| 丰县| 博湖县| 安仁县| 波密县| 甘孜| 凯里市| 河曲县| 尚义县| 治多县| 乡宁县| 诏安县| 吕梁市| 图片| 乡城县| 兰考县| 鱼台县| 平乡县| 老河口市| 托克托县| 稷山县| 郸城县| 休宁县|