Node.js and PHP, Python character processing performance comparison _node.js

Source: Internet
Author: User
Tags chr strlen

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.