Comparison of Character Processing Performance between Node. js and PHP and Python, node. jspython

Source: Internet
Author: User

Comparison of Character Processing Performance between Node. js and PHP and Python, node. jspython

Test Cases are divided into functions and classes for reading a large string of characters one by one.

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

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

Class

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

Python

Function

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

Class

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)

The content of the page.html file is a text with a length.

Test Results

Language function class Node. js 0.022 s 0.026 sPHP 0.35 s 1.02 sPython 0.58 s 1.50 s


Develop a website using php or nodejs?

I don't think node. js is not suitable for new users. After all, you have to learn PHP from scratch and have a learning process for Web programming. Therefore, the two are almost equivalent in terms of getting started. Also, you need to know about MVC and database connections.
You can compare the advantages and disadvantages of getting started.
Node. js
Advantage: js syntax is not difficult for JavaScript users to understand the code.

It simplifies the server configuration process and comes with a web server. Unlike php, apache is not required for installation and configuration.
Disadvantages: less learning materials than PHP
PHP
Advantages: many learning materials, many frameworks, and many tool kits
Disadvantage: familiar with syntax
I think this should be your first website. The importance of language is not that great. The advantages and disadvantages may not be reflected in your learning process, and they are also external factors. Of course, you can also think about it like this. Now we are focusing on PHP, and most of them are used by lightweight companies. Node has been promoted by many large companies, and more opportunities are available in the early stage.

Php string processing

The Code is as follows:

<? Php
$ Str = "my name's haha ";

Echo addslashes ($ str );
?>

Output result:

My name \'s haha

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.