Skip to content

Instantly share code, notes, and snippets.

@vls
Created November 19, 2013 03:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vls/7539716 to your computer and use it in GitHub Desktop.
Save vls/7539716 to your computer and use it in GitHub Desktop.
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