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

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

测试代码

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

函数

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

?>

<?php

class 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) . " ";

?>

Python

函数

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

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.time()

instance = Chars(content)

instance.run()

end = time.time();

print (end - start)

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

测试结果

语言 函数 类

Node.js 0.022s 0.026s

PHP 0.35s 1.02s

Python 0.58s 1.50s

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值