html5中文学习网

您的位置: 首页 > 网站及特效实例 > javascript特效 » 正文

Node.js与PHP、Python的字符处理性能对比_node.js_

[ ] 已经帮助:人解决问题

测试用例分为用函数和类来进行一个大字符串的字符逐一读取。EbFHTML5中文学习网 - HTML5先行者学习网

测试代码EbFHTML5中文学习网 - HTML5先行者学习网

Node.jsEbFHTML5中文学习网 - HTML5先行者学习网

函数EbFHTML5中文学习网 - HTML5先行者学习网

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);

EbFHTML5中文学习网 - HTML5先行者学习网

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);

PHPEbFHTML5中文学习网 - HTML5先行者学习网

函数EbFHTML5中文学习网 - HTML5先行者学习网

<?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";?>

EbFHTML5中文学习网 - HTML5先行者学习网

<?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";?>

PythonEbFHTML5中文学习网 - HTML5先行者学习网

函数EbFHTML5中文学习网 - HTML5先行者学习网

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

EbFHTML5中文学习网 - HTML5先行者学习网

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 文件内容为一个长度为 的文本。EbFHTML5中文学习网 - HTML5先行者学习网

测试结果EbFHTML5中文学习网 - HTML5先行者学习网

语言 函数 类Node.js 0.022s 0.026sPHP 0.35s 1.02sPython 0.58s 1.50s

EbFHTML5中文学习网 - HTML5先行者学习网

(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助