测试用例分为用函数和类来进行一个大字符串的字符逐一读取。
测试代码
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 ++
类
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 ++
PHP
函数
类
str = $str; $this->length = strlen($str); $this->pos = 0; } public function run(){ while($this->pos++ 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 codecs import time def chars(content): length = len(content) pos = 0 while(pos
类
import codecs import time class Chars(): def __init__(self, str): self.str = str self.length = len(str) self.pos = 0 def run(self): while(self.pos
其中 page.html 文件内容为一个长度为 的文本。
测试结果
语言 函数 类 Node.js 0.022s 0.026s PHP 0.35s 1.02s Python 0.58s 1.50s
已抢66892个
抢已抢39385个
抢已抢21818个
抢已抢2769个
抢已抢3061个
抢已抢19363个
抢