[Python-checkins] r43725 - sandbox/trunk/overload/time_overloading.py
guido.van.rossum
python-checkins at python.org
Sat Apr 8 00:36:38 CEST 2006
Author: guido.van.rossum
Date: Sat Apr 8 00:36:38 2006
New Revision: 43725
Modified:
sandbox/trunk/overload/time_overloading.py
Log:
Add an accelerated version which is faster than the manual version.
Modified: sandbox/trunk/overload/time_overloading.py
==============================================================================
--- sandbox/trunk/overload/time_overloading.py (original)
+++ sandbox/trunk/overload/time_overloading.py Sat Apr 8 00:36:38 2006
@@ -24,7 +24,7 @@
@automatic.register(C, B)
def methodCB(x, y): return "CB"
-# Quick test that it works correct
+# Quick test that it works correctly
assert automatic(C(), B()) == "CB"
assert automatic(A(), C()) == "AC"
try:
@@ -34,6 +34,22 @@
else:
assert False
+def accelerated(x, y):
+ func = automatic.cache.get((type(x), type(y)))
+ if func is not None:
+ return func(x, y)
+ return automatic(x, y)
+
+# Another quick test
+assert accelerated(C(), B()) == "CB"
+assert accelerated(A(), C()) == "AC"
+try:
+ accelerated(C(), C())
+except TypeError:
+ pass
+else:
+ assert False
+
def manual(x, y):
if isinstance(x, C):
if isinstance(y, B):
@@ -48,6 +64,11 @@
return methodAB(x, y)
return default(x, y)
+# Quick test that the manual version works correctly
+assert manual(C(), B()) == "CB"
+assert manual(A(), C()) == "AC"
+
+# Test fixture
def run(func, C1, C2):
timeit.test_func = func
timeit.test_arg1 = C1()
@@ -59,6 +80,7 @@
C2.__name__,
result)
+# Test runs
print '-'*20
run(manual, C, B)
run(manual, A, A)
@@ -70,3 +92,9 @@
run(automatic, A, A)
run(automatic, A, B)
run(automatic, A, C)
+
+print '-'*20
+run(accelerated, C, B)
+run(accelerated, A, A)
+run(accelerated, A, B)
+run(accelerated, A, C)
More information about the Python-checkins
mailing list