我正在写一个c扩展名来计算他的标准偏差。性能很重要,因为它将在大型数据集上执行。当我从列表中获得项目时,我很难弄清楚如何获得pyobject的价值。这是我第一次为python编写一个c扩展,感谢任何帮助。显然,我不知道如何正确使用代码示例按钮:(标准差的python c扩展
这是我到目前为止有:
#include <Python.h>
static PyObject*
func(PyObject *self, PyObject *args)
{
PyObject *list, *item;
Py_ssize_t i, len;
if (!PyArg_UnpackTuple(args, "func", 1, 1, &list)){
return NULL;
}
printf("hello world\n");
Py_INCREF(list);
len = PyList_GET_SIZE(list);
for (i=0;i<len;i++){
item = PyList_GET_ITEM(list, i);
PyObject_Print(item,stdout,0);
}
return list;
}
static char func_doc[] = "This function calculates standard deviation.";
static PyMethodDef std_methods[] = {
{"func", func, METH_VARARGS, func_doc},
{NULL, NULL}
};
PyMODINIT_FUNC
initstd(void)
{
Py_InitModule3("std", std_methods, "This is a sample docstring.");
}
我目前使用numpy的进行计算,但名单必须首先转换为numpy的阵列,我想,以避免由于列表很大,整个数据集是几百兆。我不是专家,但也许不计算性能需要花很多钱,但我想看看在使用Python廓线仪的一个速度上的差异。 – Xavier 2010-08-03 23:41:56
转换为numpy的阵列可能不会像你想象的那样大的交易。如果你从加载文件或数据库数据,你已经做了相当大的开销加载,作为一个普通的Python列表。如果您将数据直接加载到Numpy数组中,那么您将消除该开销。即使你有从正常列表“转换”到一个数组,你以后可以保存您numpy的阵列快速加载。 http://docs.scipy.org/doc/numpy/reference/generated/numpy.load.html – Cerin 2010-08-04 12:45:28