# Call this by 'python timer.py [-t count] [-O options]'
# The default count is 500000 and the default options are null

import sys
import re
from re import *
import time

def timer (pattern, data, repeat) :
    global result
    t = time.clock()
    for i in xrange(0,repeat) :
        result = pattern.search(data)
    t = (time.clock()-t)*1000.0/repeat
    if result :
        sys.stdout.write("Matched %s\n" % repr(result.expand(r'\g<0>')))
    else :
        sys.stdout.write("No match\n")
    sys.stdout.write('Execute time %.3f milliseconds\n' % t)
    return 0

repeat = 0
options = ''
args = sys.argv[1:]
while len(args) >= 2 :
    if args[0] == "-t" and re.match(r'^[1-9]\d*$', args[1]) and repeat == 0 :
        repeat = int(args[1])
    elif args[0] == "-O" and options == '' :
        try :
            options = eval(args[1])
            result = re.compile(r'.', options)
        except :
            result = None
        if result == None :
            sys.stderr.write("Invalid options specified\n")
            sys.exit(1)
    else :
        break
    args = args[2:]
if len(args) > 0 :
    sys.stderr.write("python timer.py [-t count] [-O options]\n")
    sys.exit(1)
if repeat == 0 :
    repeat = 500000
if options == '' :
    options = 0

while 1 :
    sys.stdout.write('Re> ')
    pattern = sys.stdin.readline()
    if not pattern :
        sys.exit(0)
    pattern = pattern[0:-1]
    try :
        t = time.clock()
        pat = re.compile(pattern, options)
        t = (time.clock()-t)*1000.0
        sys.stdout.write('Compile time %.3f milliseconds\n' % t)
        ok = 1
    except :
        sys.stdout.write('The pattern was rejected\n')
        ok = 0

    while ok :
        sys.stdout.write('Data> ')
        data = sys.stdin.readline()
        if not data :
            sys.exit(0)
        data = data[0:-1]
        if len(data) == 0 :
            break
        if timer(pat, data, repeat) :
            break
