#!/usr/bin/env python
import datetime
import os
import re
import timeit, cProfile
def strptime():
with open('time.txt', 'r') as f:
for line in f:
line = line.rstrip(os.linesep)
dt = datetime.datetime.strptime(line, "%Y-%m-%d %H:%M:%S")
def reg():
rep = re.compile(r'(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})')
with open('time.txt', 'r') as f:
for line in f:
line = line.rstrip(os.linesep)
m = rep.match(line)
dt = datetime.datetime(int(m.group(1)),
int(m.group(2)),
int(m.group(3)),
int(m.group(4)),
int(m.group(5)),
int(m.group(6))
)
if __name__ == '__main__':
t1 = timeit.Timer("reg()","from __main__ import reg")
t2 = timeit.Timer("strptime()", "from __main__ import strptime")
cProfile.run("t1.timeit(3);print")
print""
cProfile.run("t2.timeit(3);print")