Home

zhangyiqun

Thoughts, stories and ideas.

2014年以前 About Feed

06 Jan 2010
过滤字符串,小试python速度

测试方法:用python过滤出指定字符串,并和sed与grep执行时间对比

[root@jeantoe MyPy]# cat speed.py

import os, sys

testfile = open(‘/etc/passwd’)

for line in testfile:

if ‘root’ in line:

print line

[root@jeantoe MyPy]# time python speed.py

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

**real    0m0.033s

user    0m0.024s

sys    0m0.009s**

[root@jeantoe MyPy]# time grep root /etc/passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

**real    0m0.003s

user    0m0.000s

sys    0m0.003s**

[root@jeantoe MyPy]# time sed -n ‘/root/p’ /etc/passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

**real    0m0.003s

user    0m0.001s

sys    0m0.001s**


zhangyiqun

scribble

2014年以前 About Feed