Optimisation problem - in C?

Dave Cole djc at object-craft.com.au
Wed Feb 21 05:54:29 CET 2001


Forgot to followup in the newsgroup...

>>>>> "Tim" == Tim Churches <tchur at optushome.com.au> writes:

Tim> def makevector(sourcelist,elementlist):
Tim>         resultvector = []
Tim>         for element in elementlist:
Tim>                 resultvector.append(sourcelist[element])
Tim>         return resultvector

Because I am trying to avoid writing documentation for some code that
I want to give away, I decided to whip up some code for you to try.

Extract the two following files and grab a copy of Makefile.pre.in
from any other extension module - the CSV module from

        http://www.object-craft.com.au/projects/csv/

is where I copied mine from.

Place all of the files in a directory then type:

% make -f Makefile.pre.in boot
% make
% python
Python 1.5.2 (#0, Apr  3 2000, 14:46:48)  [GCC 2.95.2 20000313 (Debian GNU/Linux)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import timbo
>>> a = ['a','b','c','d','e']
>>> b = [2,3,4]
>>> timbo.makevector(a, b)
['c', 'd', 'e']
>>> 

Tell me if it goes faster (or if it even works :-)

Because I am a bit lazy, I have assumed that elementlist is a sequence
of integers.

- Dave

- - 8< - Setup.in - - - - - - - - - - - - - - - - - - - - - - - - - -
# Setup file to build the timbo module

*shared*
timbo timbo.c
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

- - 8< - timbo.c - - - - - - - - - - - - - - - - - - - - - - - - - - -
#include "Python.h"

static char makevector__doc__[] =
"Works just like the following - only faster (I hope)\n"
"\n"
"def makevector(sourcelist, elementlist):\n"
"	 resultvector = []\n"
"	 for element in elementlist:\n"
"		 resultvector.append(sourcelist[element])\n"
"	 return resultvector";

static PyObject *makevector(PyObject *module, PyObject *args)
{
    PyObject *sourcelist, *elementlist;
    PyObject *resultlist = NULL;
    int len, i;

    if (!PyArg_ParseTuple(args, "OO", &sourcelist, &elementlist))
	return NULL;

    len = PySequence_Length(elementlist);
    if (PyErr_Occurred())
	return NULL;

    resultlist = PyList_New(len);
    if (resultlist == NULL)
	return NULL;

    for (i = 0; i < len; i++) {
	PyObject *item;
	int num;

	if ((item = PySequence_GetItem(elementlist, i)) == NULL)
	    goto error;
	num = PyInt_AsLong(item);
	if (PyErr_Occurred()) {
	    Py_DECREF(item);
	    goto error;
	}
	if ((item = PySequence_GetItem(sourcelist, num)) == NULL)
	    goto error;
	PyList_SetItem(resultlist, i, item);
    }

    return resultlist;

error:
    Py_DECREF(resultlist);
    return NULL;
}

/* List of methods defined in the module */
static struct PyMethodDef methods[] = {
    { "makevector", (PyCFunction)makevector, METH_VARARGS, makevector__doc__ },
    { NULL, (PyCFunction)NULL, 0, NULL } /* sentinel */
};

static char module__doc__[] = 
"Tim's optimisations.";

void inittimbo(void)
{
    Py_InitModule4("timbo", methods, module__doc__,
		   (PyObject*)NULL, PYTHON_API_VERSION);
    if (PyErr_Occurred())
	Py_FatalError("can't initialize module timbo");
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

-- 
http://www.object-craft.com.au



More information about the Python-list mailing list