[Python-Dev] Silly little benchmark

Guido van Rossum guido@digicool.com
Fri, 13 Jul 2001 16:25:48 -0400


Here's a patch to abstract.c that does to binary_op1() what I had in
mind.  My own attempts at timing this only serve to confuse me, but
I'm sure the experts will be able to assess it.  I think it may make
pystone about 1% faster.

Note that this assumes that a type object only sets the
NEW_STYLE_NUMBER flag when it has a non-NULL tp_as_number structure
pointer.  This makes sense, but just to be sure I add an assert().

In a bizarre twist of benchmarking, if I comment the asserts out,
pystone is 1% *slower* than without the patch....  I guess I'm going
to ignore that.

Enjoy.

--Guido van Rossum (home page: http://www.python.org/~guido/)

Index: abstract.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/abstract.c,v
retrieving revision 2.60.2.5
diff -c -r2.60.2.5 abstract.c
*** abstract.c	2001/07/07 22:55:30	2.60.2.5
--- abstract.c	2001/07/13 20:14:01
***************
*** 318,324 ****
  {
  	PyObject *x;
  	binaryfunc *slot;
! 	if (v->ob_type->tp_as_number != NULL && NEW_STYLE_NUMBER(v)) {
  		slot = NB_BINOP(v->ob_type->tp_as_number, op_slot);
  		if (*slot) {
  			x = (*slot)(v, w);
--- 318,334 ----
  {
  	PyObject *x;
  	binaryfunc *slot;
! 
! 	/* Quick test if anything down here could work */
! 	if (v->ob_type->tp_as_number == NULL &&
! 	    w->ob_type->tp_as_number == NULL)
! 	{
! 		Py_INCREF(Py_NotImplemented);
! 		return Py_NotImplemented;
! 	}
! 
! 	if (NEW_STYLE_NUMBER(v)) {
! 		assert (v->ob_type->tp_as_number != NULL);
  		slot = NB_BINOP(v->ob_type->tp_as_number, op_slot);
  		if (*slot) {
  			x = (*slot)(v, w);
***************
*** 331,337 ****
  			goto binop_error;
  		}
  	}
! 	if (w->ob_type->tp_as_number != NULL && NEW_STYLE_NUMBER(w)) {
  		slot = NB_BINOP(w->ob_type->tp_as_number, op_slot);
  		if (*slot) {
  			x = (*slot)(v, w);
--- 341,348 ----
  			goto binop_error;
  		}
  	}
! 	if (NEW_STYLE_NUMBER(w)) {
! 		assert (w->ob_type->tp_as_number != NULL);
  		slot = NB_BINOP(w->ob_type->tp_as_number, op_slot);
  		if (*slot) {
  			x = (*slot)(v, w);