Node.js与PHP、Python的字符处理性能对比

来源:互联网 发布:linux下安装中文字体 编辑:程序博客网 时间:2024/04/29 17:43

测试用例分为用函数和类来进行一个大字符串的字符逐一读取。

    测试代码

    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 ++ < length){  var chr = content[pos - 1]; }}var start = Date.now();chars(content);var end = Date.now();console.log(end - start);

    类

   

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

    函数

   

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

    类

   

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

    函数

   

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

    类

   

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)

    其中 page.html 文件内容为一个长度为 的文本。

    测试结果

   

语言 函数 类Node.js 0.022s 0.026sPHP 0.35s 1.02sPython 0.58s 1.50s

   

您可能感兴趣的文章:

  • 提高NodeJS中SSL服务的性能
  • 为高负载网络优化Nginx和Node.js的方法
  • nodejs的10个性能优化技巧

    QQ空间 新浪微博 腾讯微博 搜狐微博 人人网 开心网 百度搜藏更多

    Tags:Node.js HP Python

    复制链接收藏本文打印本文关闭本文返回首页

    上一篇:基于promise.js实现nodejs的promises库

    下一篇:nodejs的10个性能优化技巧

   

相关文章

  • 2014-06-06nodejs实现黑名单中间件设计
  • 2014-06-06node.js WEB开发中图片验证码的实现方法
  • 2014-06-06nodejs获取本机内网和外网ip地址的实现代码
  • 2014-06-06node.js入门教程
  • 2014-06-06Node.js中require的工作原理浅析
  • 2014-06-06node.js实现多图片上传实例
  • 2014-07-07nodejs的10个性能优化技巧
  • 2014-07-07提高NodeJS中SSL服务的性能
  • 2014-07-07我的Node.js学习之路(三)--node.js作用、回调、同步和异步代码
  • 2014-06-06node.js正则表达式获取网页中所有链接的代码实例

   

文章评论

   

最 近 更 新

   

  • node.js入门教程
  • connect中间件session、cookie的使用方法
  • node.js正则表达式获取网页中所有链接的代
  • 使用GruntJS构建Web程序之Tasks(任务)篇
  • 使用GruntJS构建Web程序之合并压缩篇
  • Node.js中对通用模块的封装方法
  • nodejs npm包管理的配置方法及常用命令介
  • Express作者TJ告别Node.js奔向Go
  • 我的Node.js学习之路(二)NPM模块管理
  • Node.js中require的工作原理浅析

   

热 点 排 行

   

  • nodejs文件操作模块FS(File Sys
  • Nodejs sublime text 3安装与配置
  • nodejs获取本机内网和外网ip地址
  • node.js WEB开发中图片验证码的实
  • NODE.JS加密模块CRYPTO常用方法介
  • Node.js(安装,启动,测试)
  • nodejs npm install全局安装和本
  • node.js应用后台守护进程管理器F
  • node.js实现多图片上传实例
  • node.js实现逐行读取文件内容的代
0 0
原创粉丝点击