此脚本找到large_re_file.txt的行数以及pDq的行数。
-
[root@oracle11g test]# cat re_loop_nocompile.py
-
#!/usr/bin/env python
-
-
import re
-
-
def run_re():
-
pattern = 'pDq'
-
-
infile = open('large_re_file.txt', 'r');
-
match_count = 0
-
lines = 0
-
for line in infile:
-
match = re.search(pattern, line)
-
if match:
-
match_count += 1
-
lines += 1
-
return (lines, match_count)
-
-
if __name__ == "__main__":
-
lines, match_count = run_re()
-
print 'LINES::',lines
-
print 'MATCHES::',match_count
-
large_re_file.txt 里面的内容如下:
[root@oracle11g test]#
cat large_re_file.txt
pDq
pattern
执行脚本如下:
[root@oracle11g test]#
./re_loop_nocompile.py
LINES:: 2
MATCHES:: 1
ipython运行loop的调用时间
[root@oracle11g test]#
ipython
/usr/lib/python2.6/site-packages/IPython/Magic.py:38: DeprecationWarning: the sets module is deprecated
from sets import Set
Python 2.6.6 (r266:84292, May 1 2012, 13:52:17)
Type "copyright", "credits" or "license" for more information.
IPython 0.9.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]:
import re_loop_nocompile
In [2]:
timeit -n 5 re_loop_nocompile.run_re()
5 loops, best of 3: 22.6 ?s per loop
调用函数5次,每次22.6us
使用unix/linux脚本测试时间
[root@oracle11g test]#
time python re_loop_nocompile.py
LINES:: 2
MATCHES:: 1
real 0m0.030s
user 0m0.022s
sys 0m0.009s
阅读(3254) | 评论(0) | 转发(0) |