Skip to content

Instantly share code, notes, and snippets.

@vls

vls/test_cffi_re2.py

Created Nov 19, 2013
Embed
What would you like to do?
comparison between cffi_re2 and builtin re module
#!/usr/bin/env python
import sys
import time
def test_re():
print 'test python re'
import re
arr = []
with open('regex.txt') as f:
for line in f:
s = line.rstrip('\r\n')
s = s.decode('utf-8')
arr.append(re.compile(s))
t0 = time.time()
count_match = 0
with open('test_str.txt') as f:
for line in f:
line = line.decode('utf-8')
for sobj in arr:
m = sobj.search(line)
if m:
count_match += 1
eltime = time.time() - t0
print 'python re', eltime
print 'count match', count_match
return eltime
def test_cffi():
print 'test cffi'
import cffi_re2
arr = []
with open('regex.txt') as f:
for line in f:
s = line.rstrip('\r\n')
arr.append(cffi_re2.compile(s))
t0 = time.time()
count_match = 0
with open('test_str.txt') as f:
for line in f:
for sobj in arr:
m = sobj.search(line)
if m:
count_match += 1
eltime = time.time() - t0
print 'cffi re2', eltime
print 'count match', count_match
return eltime
if __name__ == '__main__':
repeat = 3
if len(sys.argv) >= 2 and sys.argv[1] == 'cffi':
test_cffi()
total = 0
for i in xrange(repeat):
total += test_cffi()
print 'avg time', total * 1.0 / repeat
else:
test_re()
total = 0
for i in xrange(repeat):
total += test_re()
print 'avg time', total * 1.0 / repeat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment