Skip to content

Commit

Permalink
provide a sample test to benchmark the impact of caching the dates
Browse files Browse the repository at this point in the history
As a reference on a laptop with :
Intel(R) Core(TM) i7-2630QM CPU @ 2.00GHz
Python 2.7.7rc1
Debian SID - Linux 3.14-1-amd64 matomo-org#1 SMP Debian 3.14.4-1 (2014-05-13) x86_64 GNU/Linux

The results are :
cache   : 647.914 ms
nocache : 21440.542 ms
  • Loading branch information
cbonte committed Jun 2, 2014
1 parent 3b2d328 commit 3b33a37
Show file tree
Hide file tree
Showing 2 changed files with 1,000,046 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tests/debug/bench_date_cache.py
@@ -0,0 +1,46 @@
#!/usr/bin/env python

import datetime
import time
from collections import OrderedDict

date_format = '%d/%b/%Y:%H:%M:%S'

def timing(f):
def wrap(*args):
time1 = time.time()
ret = f(*args)
time2 = time.time()
print '%s\t: %0.3f ms' % (f.func_name, (time2-time1)*1000.0)
return ret
return wrap

@timing
def nocache():
file = open("timestamps.log", 'r')
for lineno, line in enumerate(file):
date_string, timezone_string = line.split()

date = datetime.datetime.strptime(date_string, date_format)
timezone = float(timezone_string)

date -= datetime.timedelta(hours=timezone/100)

@timing
def cache():
cache_dates = OrderedDict()
file = open("timestamps.log", 'r')
for lineno, line in enumerate(file):
date_string, timezone_string = line.split()
key = line

date = cache_dates.get(key)
if not date:
date = datetime.datetime.strptime(date_string, date_format)
timezone = float(timezone_string)

date -= datetime.timedelta(hours=timezone/100)
cache_dates[key] = date

cache()
nocache()

0 comments on commit 3b33a37

Please sign in to comment.