在64位Windows上的嵌入式Python3.4中添加新模块时,速度慢且内存高

2024-09-26 22:51:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要让我的Python程序访问我的C函数。所以我用example from the documentation开始

不幸的是,它非常慢,占用了我所有的记忆

我使用的是64位windows7、python3.4和gcc(rubenvb-4.8.0)4.8.0

为完整起见,以下是文档中的代码:

#include <Python.h>

static int numargs=0;

static PyObject*
emb_numargs(PyObject *self, PyObject *args)
{
    if(!PyArg_ParseTuple(args, ":numargs"))
        return NULL;
    return PyLong_FromLong(numargs);
}

static PyMethodDef EmbMethods[] = {
    {"numargs", emb_numargs, METH_VARARGS,
     "Return the number of arguments received by the process."},
    {NULL, NULL, 0, NULL}
};

static PyModuleDef EmbModule = {
    PyModuleDef_HEAD_INIT, "emb", NULL, -1, EmbMethods,
    NULL, NULL, NULL, NULL
};

static PyObject*
PyInit_emb(void)
{
    return PyModule_Create(&EmbModule);
}

int main()
{
    [...]
    numargs = argc;
    PyImport_AppendInittab("emb", &PyInit_emb);

    Py_Initialize();
    pModule = PyImport_Import(pName);
    [...]
}

python程序具有以下代码:

import emb
print("Number of arguments", emb.numargs())

Tags: ofthe代码程序returnargsstaticarguments

热门问题