กรณีทดสอบแบ่งออกเป็นฟังก์ชั่นและคลาสเพื่ออ่านอักขระของสตริงขนาดใหญ่ทีละตัว
รหัสทดสอบ
node.js
การทำงาน
var fs = ต้องการ ("fs"); var content = fs.readfilesync ("page.html", {การเข้ารหัส: "utf-8"}); ฟังก์ชั่นตัวอักษร (เนื้อหา) {var length = content.length; var pos = 0; ในขณะที่ (pos ++ <ความยาว) {var chr = content [pos - 1]; }} var start = date.now (); chars (เนื้อหา); var end = date.now (); console.log (end - start);ใจดี
var fs = ต้องการ ("fs"); var content = fs.readfilesync ("page.html", {การเข้ารหัส: "utf-8"}); var chars = function (str) {this.str = str; this.length = str.length this.pos = 0;} chars.prototype.run = function () {ในขณะที่ (this.pos ++ <this.length) {var chr = this.str [this.pos - 1]; }} var start = date.now (); var instance = chars ใหม่ (เนื้อหา); instance.run (); var end = date.now (); console.log (end - start);PHP
การทำงาน
<? phpfunction chars ($ content) {$ length = strlen ($ เนื้อหา); $ pos = 0; ในขณะที่ ($ pos ++ <$ ความยาว) {$ char = $ content {$ pos - 1}; }} $ content = file_get_contents ("page.html"); $ start = microtime (จริง); chars ($ content); $ end = microtime (จริง); echo ($ end - $ start) "/n";?>ใจดี
<? phpclass chars {ฟังก์ชั่นสาธารณะ __construct ($ str) {$ this-> str = $ str; $ this-> length = strlen ($ str); $ this-> pos = 0; } ฟังก์ชั่นสาธารณะ Run () {ในขณะที่ ($ this-> pos ++ <$ this-> ความยาว) {$ char = $ this-> str {$ this-> pos-1}; }}} $ content = file_get_contents ("page.html"); $ start = microtime (จริง); $ อินสแตนซ์ = Chars ใหม่ ($ เนื้อหา); $ อินสแตนซ์ -> run (); $ end = microtime (จริง); echo ($ end - $ start) "/n";?>งูหลาม
การทำงาน
นำเข้า codecsimport chars timedef (เนื้อหา): ความยาว = len (เนื้อหา) pos = 0 ในขณะที่ (pos <ความยาว): char = content [pos] pos = pos + 1f = codecs.open ('page.html', encoding = 'utf -8') เนื้อหา = f.read ()ใจดี
นำเข้า codecsimport timeclass chars (): def __init __ (ตัวเอง, str): self.str = str self.length = len (str) self.pos = 0 def run (self): ในขณะที่ (self.pos <self.length): char = self.str [self.pos] self.pos.pos.pos + 1f = codecs.pen การเข้ารหัส = 'utf -8') เนื้อหา = f.read () start = time.time () อินสแตนซ์ = chars (เนื้อหา) อินสแตนซ์ () end = time.time (); พิมพ์ (สิ้นสุด - เริ่มต้น)
เนื้อหาไฟล์ page.html เป็นข้อความที่มีความยาว
ผลการทดสอบ
ฟังก์ชั่นภาษาคลาสโหนด. JS 0.022S 0.026SPHP 0.35S 1.02Spython 0.58S 1.50S