#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# see benchsimple.py
# manually written python code (for Python3)
#
# probably nearly fastest possible...
# Note: escape consumes about 1/3..1/2 of the time
import time, sys
from benchsimple import test_results, context_dict, N
#----------------------
# evaluation
cache={}
def _eval(expr, a=None):
if expr not in cache:
cache[expr] = compile(expr,"","eval")
return eval(cache[expr],{"__builtins__":None},nsDict)
#----------------------
# different escapeing-routines
(HTML, LATEX) = range(0, 2)
#import cgi
#import xml.sax.saxutils
#from xml.sax.saxutils import escape,quoteattr
def escape(s, format=HTML):
# return cgi.escape(s, True) #is slower
# return xml.sax.saxutils.escape(s) #s
# return xml.sax.saxutils.escape(s,{'"':""","'":"&abc;"}) #s
s = s.replace("&", "&") # must be done first!
s = s.replace("<", "<")
s = s.replace(">", ">")
s = s.replace('"', """)
s = s.replace("'", "'")
return s
#escape = escape_ #0.106..8, 0.0675..69, <>&"'
#escape = cgi.escape #0.092..4, 0.061..2 <>&
#escape = xml.sax.saxutils.escape #0.095..6, 0.063..67 <>&
#cgi..., xml... are both slower if wrapped into escape-function
myDict = context_dict.copy()
#----------------------
# different handcoded versions
result_header = '''
'''
def handcoded_eval_escape(nsDict):
myDict.update(nsDict)
l = []
write = l.append
write(result_header)
write(''' Benchsimple | ''')
write(escape(_eval("title")))
write('''
Benchsimple | ''')
write(escape(_eval("title")))
write('''
''' )
for i in enumerate(_eval("table")):
nsDict["n"] =i[0]
nsDict["row"]=i[1]
write( '''
''' )
for col in _eval("row"):
nsDict["col"]=col
if col == nsDict["highlight"]:
write( ''' ''' )
else:
write( ''' | ''' )
write( escape(str(_eval("col"))) )
write( ''' | \n''' )
write( '''
\n''' )
write( '''
''' )
return "".join(l)
def handcoded_eval_noescape(nsDict):
myDict.update(nsDict)
l = []
write = l.append
write(result_header)
write(''' Benchsimple | ''')
write(_eval("title"))
write('''
Benchsimple | ''')
write(_eval("title"))
write('''
''' )
for i in enumerate(_eval("table")):
nsDict["n"] =i[0]
nsDict["row"]=i[1]
write( '''
''' )
for col in _eval("row"):
nsDict["col"]=col
if col == nsDict["highlight"]:
write( ''' ''' )
else:
write( ''' | ''' )
write( str(_eval("col")) )
write( ''' | \n''' )
write( '''
\n''' )
write( '''