[zopeorg-checkins] CVS: Products/PythonMethod/zbytecodehacks/tests - Makefile:1.1 __init__.py:1.1 __init__.pyo:1.1 bench.py:1.1 inline_test.py:1.1 inline_test.pyo:1.1 macro_test.py:1.1 test.py:1.1 utils.py:1.1 utils.pyo:1.1

Sidnei da Silva sidnei at x3ng.com.br
Fri May 30 11:17:53 EDT 2003


Update of /cvs-zopeorg/Products/PythonMethod/zbytecodehacks/tests
In directory cvs.zope.org:/tmp/cvs-serv19195/PythonMethod/zbytecodehacks/tests

Added Files:
	Makefile __init__.py __init__.pyo bench.py inline_test.py 
	inline_test.pyo macro_test.py test.py utils.py utils.pyo 
Log Message:
Adding products needed for migration of NZO

=== Added File Products/PythonMethod/zbytecodehacks/tests/Makefile ===
clean:
	$(RM) *~ *.pyc
	


=== Added File Products/PythonMethod/zbytecodehacks/tests/__init__.py ===


=== Added File Products/PythonMethod/zbytecodehacks/tests/__init__.pyo ===
™N
;67c       s   d  S(   N(    (    s6   /home/mwh21/src/python/bytecodehacks/tests/__init__.pys   ?  s    

=== Added File Products/PythonMethod/zbytecodehacks/tests/bench.py ===
import closure

g1 = g2 = g3 = g4 = g5 = 0

def func0(x):
    return x + 0 + 0 + 0 + 0 + 0

def func01(x):
    return x

def func1(x, g1=g1, g2=g2, g3=g3, g4=g4, g5=g5):
    return x + g1 + g2 + g3 + g4 + g5
    
def func2(x):
    return x + g1 + g2 + g3 + g4 + g5

func3 = closure.bind_now(func2)

def timing(func, args, n=1, **keywords) :
	import time
	time=time.time
	appl=apply
	if type(args) != type(()) : args=(args,)
	rep=range(n)
	before=time()
	for i in rep: res=appl(func, args, keywords)
	return round(time()-before,4), res

def test():
    res = [0,0,0,0,0]
    for i in range(10):
        res[0]=res[0]+timing(func0, 42, 100000)[0]
        res[1]=res[1]+timing(func01, 42, 100000)[0]
        res[2]=res[2]+timing(func1, 42, 100000)[0]
        res[3]=res[3]+timing(func2, 42, 100000)[0]
        res[4]=res[4]+timing(func3, 42, 100000)[0]
    print res
    
result = """
>>> test()
func1 (1.54, 42)
func2 (1.98, 42)
func2 (1.59, 42)
"""


=== Added File Products/PythonMethod/zbytecodehacks/tests/inline_test.py ===
from bytecodehacks import inline,macro

def f(x,y):
    return x+y

def f2((x),(y)):
    return x+y

def integ_global(dx,x0,x1,y):
    x=x0
    while x<x1:
        dy=f(x,y)
        y=y+dx*dy
        x=x+dx
    return y

# modulo SET_LINENOs integ_inline2 has *exactly*the*same* bytecodes as
# integ_hand - and so is the same in python -O mode. I'm quite proud
# of that...
integ_inline2=macro.expand_these(integ_global,f=f2)

def integ_local(dx,f,x0,x1,y):
    x=x0
    while x<x1:
        dy=f(x,y)
        y=y+dx*dy
        x=x+dx
    return y

integ_inline=inline.inline(integ_global,f=f)

def integ_hand(dx,x0,x1,y):
    x=x0
    while x<x1:
        dy=x+y
        y=y+dx*dy
        x=x+dx
    return y

def test(param = 0.00001):
    import time
    T=time.time
    t=T(); integ_global(param,0,1,1); gf=T()-t
    print "        global function call: ",gf
    t=T(); integ_local(param,f,0,1,1); lf=T()-t
    print "         local function call: ",lf
    t=T(); integ_inline(param,0,1,1); ifc=T()-t
    print "       inlined function call: ",ifc
    t=T(); integ_inline2(param,0,1,1); ifc2=T()-t
    print "      inlined2 function call: ",ifc2
    t=T(); integ_hand(param,0,1,1); hif=T()-t
    print "  hand inlined function call: ",hif
    print "        global function call %010.10f %010.10f %010.10f %010.10f %010.10f"%(gf  /gf  ,gf  /lf  ,gf  /ifc ,gf  /ifc2,gf  /hif )
    print "         local function call %010.10f %010.10f %010.10f %010.10f %010.10f"%(lf  /gf  ,lf  /lf  ,lf  /ifc ,lf  /ifc2,lf  /hif )
    print "       inlined function call %010.10f %010.10f %010.10f %010.10f %010.10f"%(ifc /gf  ,ifc /lf  ,ifc /ifc ,ifc /ifc2,ifc /hif )
    print "      inlined2 function call %010.10f %010.10f %010.10f %010.10f %010.10f"%(ifc2/gf  ,ifc2/lf  ,ifc2/ifc ,ifc2/ifc2,ifc2/hif )
    print "  hand inlined function call %010.10f %010.10f %010.10f %010.10f %010.10f"%(hif /gf  ,hif /lf  ,hif /ifc ,hif /ifc2,hif /hif )

import profile
def longt(n):
    for i in range(n):
        g=macro.expand_these(integ_global,f=f2)

def p(n):
    pr = profile.Profile()
    pr.runctx('longt(%d)'%n,globals(),locals())
    pr.print_stats()


=== Added File Products/PythonMethod/zbytecodehacks/tests/inline_test.pyo ===
  <Binary-ish file>

=== Added File Products/PythonMethod/zbytecodehacks/tests/macro_test.py ===
from bytecodehacks import macro

def incr((x)):
    x = x + 1
    return x

def postincr((x)):
    t = x
    x = x + 1
    return t

macro.add_macro(incr)

def f(x):
    while incr(x) < 10:
        print x

ff = macro.expand(f)
gg = macro.expand_these(f,incr=postincr)



=== Added File Products/PythonMethod/zbytecodehacks/tests/test.py ===
from bytecodehacks import code_editor,ops

def f():
    return "This function does something really interesting"
    
g=code_editor.Function(f)
g.func_code.co_code[:]=[ops.SET_LINENO(3),
                        ops.LOAD_CONST(len(g.func_code.co_consts)),
                        ops.RETURN_VALUE()]
g.func_code.co_consts.append("Not any more!")
g=g.make_function()
print f()
print g()

from bytecodehacks import attr_freeze
import sys

def ff(x):
    if x:
        print sys.exit
    else:
        print sys.copyright

_=attr_freeze.Ref()

gg=attr_freeze.freeze_attrs(ff,
                            _.sys.exit,sys.exit,
                            _.sys.copyright,sys.copyright)


=== Added File Products/PythonMethod/zbytecodehacks/tests/utils.py ===
import dis,tempfile,sys,os

def diff_funcs(f1,f2):
    file1 = tempfile.mktemp()
    file2 = tempfile.mktemp()

    svstdout = sys.stdout

    try:
        sys.stdout = open(file1, 'w')
        dis.dis(f1)

        sys.stdout = open(file2, 'w')
        dis.dis(f2)

        sys.stdout = svstdout

        os.system("diff -y %s %s"%(file1,file2))
    finally:
        sys.stdout = svstdout
        os.unlink(file1)
        os.unlink(file2)



=== Added File Products/PythonMethod/zbytecodehacks/tests/utils.pyo ===
  <Binary-ish file>




More information about the zopeorg-checkins mailing list