Node.js与PHP、Python的字符处理性能对比_node.js
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Node.js与PHP、Python的字符处理性能对比_node.js,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1881字,纯文字阅读大概需要3分钟。
内容图文
测试用例分为用函数和类来进行一个大字符串的字符逐一读取。测试代码
Node.js
函数
类
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) . "\n"; ?>
类
<?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) . "\n"; ?>
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
内容总结
以上是互联网集市为您收集整理的Node.js与PHP、Python的字符处理性能对比_node.js全部内容,希望文章能够帮你解决Node.js与PHP、Python的字符处理性能对比_node.js所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。