Test cases are divided into characters that use functions and classes to read a large string.
Test code
Node.js
Function
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);
Class
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
Function
<?php
function 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";
? >
Class
<?php
class chars{public
function __construct ($str) {
$this->str = $str;
$this->length = strlen ($STR);
$this->pos = 0;
}
The 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
Function
Import codecs
import time
def chars (content):
length = len (content)
pos = 0 while
(POS < Length):
char = content[pos]
pos = pos + 1
f = codecs.open (' page.html ', encoding= ' utf-8 ')
content = f . Read ()
start = Time.time ()
chars (content) End
= Time.time ();
Print End-start
Class
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 < Self.length):
char = Self.str[self.pos]
Self.pos = Self.pos + 1
f = codecs.open (' page.html ', encoding= ' utf-8 ')
content = F.read ()
start = Time.tim E ()
instance = Chars (content)
Instance.run () End
= Time.time ();
Print (End-start)
Where the page.html file content is a length of text.
Test results
Language function class
node.js 0.022s 0.026s
PHP 0.35s 1.02s
Python 0.58s 1.50s