[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Py C/API - Using METH_O vs METH_VARARGS



On Tue, Dec 23, 2008 at 9:53 AM, Lenard Lindstrom <len-l@xxxxxxxxx> wrote:
> Peter Shinners wrote:
>>
>> Campbell Barton wrote:
>>>
>>> Noticed METH_O isnt used anywhere in pygame, this avoids creating a
>>> tuple only to get a single PyObject back out with PyArg_ParseTuple()
>>
>> I'm pretty sure METH_O has been added after Python 2.3. Someone should
>> definitely find the minimum required Python version before switching
>> anything.
>
> I checked the online Python documents and METH_O is present in Python 2.2.
> That is the earliest Python version with current Pygame development, on the
> Nokia S60. I personally was going to do a timing test to see if METH_O made
> a significant performance difference. If not then it is not worth the bother
> making the changes for ascetic reasons alone.
>
> --
> Lenard Lindstrom
> <len-l@xxxxxxxxx>
>
>

From a loop of 10,000,000, heres my timings

pass 1.85659885406
METH_NOARGS 3.24079704285
METH_O 3.66321516037
METH_VARARGS 6.09881997108
METH_KEYWORDS 6.037307024
METH_KEYWORDS (as keyword) 10.9263861179

------- Python Script, (used blender module for testing)

import time

from Blender.sys import test_METHO, test_METH_VARARGS,
test_METH_KEYWORDS, test_METH_NOARGS

RUN = 10000000



t = time.time()

for i in xrange(RUN):	pass

print 'pass', time.time()-t



t = time.time()

for i in xrange(RUN):	test_METH_NOARGS()

print 'METH_NOARGS', time.time()-t



t = time.time()

for i in xrange(RUN):	test_METHO(1)

print 'METH_O', time.time()-t



t = time.time()

for i in xrange(RUN):	test_METH_VARARGS(1)

print 'METH_VARARGS', time.time()-t



t = time.time()

for i in xrange(RUN):	test_METH_KEYWORDS(1)

print 'METH_KEYWORDS', time.time()-t



t = time.time()

for i in xrange(RUN):	test_METH_KEYWORDS(val=1)

print 'METH_KEYWORDS (as keyword)', time.time()-t

------------------- C functions

static PyObject *test_METHO( PyObject * self, PyObject * value )
{
	int val = (int)PyInt_AsLong(value);
	
	if( val==-1 && PyErr_Occurred() ) {
		PyErr_SetString(PyExc_AttributeError, "not an int");
		return NULL;
	}
	
	Py_RETURN_NONE;
}

static PyObject *test_METH_VARARGS( PyObject * self, PyObject * args )
{
	int val;
	
	if( !PyArg_ParseTuple( args, "i", &val ) )
		return NULL;
	
	Py_RETURN_NONE;
}

static PyObject *test_METH_KEYWORDS( PyObject * self, PyObject * args,
PyObject *kwd)
{
	int val;
	static char *kwlist[] = {"val", NULL};
	
	if( !PyArg_ParseTupleAndKeywords(args, kwd, "i", kwlist, &val) )
		return NULL;
	
	Py_RETURN_NONE;
}

static PyObject *test_METH_NOARGS( PyObject * self, PyObject * args )
{
	Py_RETURN_NONE;
}

struct PyMethodDef M_sys_methods[] = {
	{"test_METHO", test_METHO, METH_O, ""},
	{"test_METH_KEYWORDS", test_METH_KEYWORDS, METH_KEYWORDS, ""},
	{"test_METH_NOARGS", test_METH_NOARGS, METH_NOARGS, ""},
	{"test_METH_VARARGS", test_METH_VARARGS, METH_VARARGS, ""},
	{NULL, NULL, 0, NULL}
};

-- 
- Campbell