python re模块编译代码与非编译代码性能检测
来源:互联网 发布:好的c语言集成开发环境 编辑:程序博客网 时间:2024/05/14 19:10
首先创建1000万条测试数据,如下所示
统计一下行数
### 一:re非编译代码性能检测:
import redef run_re(): pattern='12' infile=open('cs.txt','r') matchcount=0 lines=0 for line in infile: match=re.search(pattern, line) if match: matchcount+=1 lines+=1 return(lines,matchcount)if __name__=="__main__": lines,matchcount=run_re() print lines print matchcount
用ipython的timeit工具检测花费时间
timeit工具执行一段代码数次,然后报告最佳运行所花费的时间。
从上可以看出,运行10次,最佳运行花费18.1s
###二:re编译代码性能检测
import redef run_re(): pattern='12' re_obj=re.compile(pattern) infile=open('cs.txt','r') matchcount=0 lines=0 for line in infile: match=re._obj.search(line) if match: matchcount+=1 lines+=1 return(lines,matchcount)if __name__=="__main__": lines,matchcount=run_re() print lines print matchcount
同样用timeit检测花费时间:
从上可以看出,运行10次,最佳运行花费5.23s
总结:通过对比,编译版本优越性很大。
- python re模块编译代码与非编译代码性能检测
- 编译C代码为python模块(pyd)
- python 检测代码性能
- 编译python代码方法
- 编译代码与Tarball
- Linux 编译SSD Caffe目标检测代码
- 删除非编译的.c文件代码
- xcode编译代码到非测试机
- 代码编译
- 程序编译与代码优化
- WRF-DA代码编译与安装(二)——WRF-DA模块的编译与安装
- 托管代码与非托管代码的性能比较
- 编写适应Unicode与非Unicode编译环境的代码的几点经验
- 在 Xcode 里编译运行 Python 代码
- python模块-re模块
- python中正则表达式与re 模块
- python中正则表达式与re 模块
- python与正则表达式 re模块
- substr截取中文字符出现乱码的解决方案
- linux抢占式内核的描述
- C++中extern “C”含义深层探索
- ANT学习
- 父元素绝对定位 子元素在IE6 不能点击
- python re模块编译代码与非编译代码性能检测
- android 获取手机和SD卡可用内存
- java 内存泄露
- UNICODE,GBK,UTF-8区别
- 注册PostSharp
- 18个最佳代码编辑器/IDE推荐
- IE6、7 按钮文字垂直居中 按钮文字在IE下的兼容性方案
- ANT的安装
- 学成绩不佳的数学大师─埃尔米特 (Hermite)