Page 244
main.c
#define PY_SSIZE_T_CLEAN
#include <Python.h>
int main(int argc, char *argv[])
{
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print('Today is', ctime(time()))\n");
Py_FinalizeEx();
return 0;
}
tasks.json WINDOWS
Change user name
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/nologo",
"/IC:/Users/mike.james/AppData/Local/Programs/Python/Python311/include",
"/Fe${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}",
"/link /LIBPATH:C:/Users/mike.james/AppData/Local/Programs/Python/Python311/libs"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
tasks.json LINUX
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc-10 build active file",
"command": "/usr/bin/gcc-10",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-I/usr/local/include/python3.11",
"-L/usr/local/lib/python3.11/config-3.11-arm-linux-gnueabihf",
"-L/usr/local/lib",
"-Xlinker",
"-export-dynamic",
"-lpython3.11",
"-lpthread",
"-ldl",
"-lutil",
"-lm",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Page 247
#define PY_SSIZE_T_CLEAN
#include <Python.h>
static PyObject *Pi(PyObject *self, PyObject *args)
{
int m, n;
double pi, s;
if (!PyArg_ParseTuple(args, "ii", &m, &n))
return NULL;
pi = 0;
for (int k = m; k < n; k++)
{
s = 1;
if (k % 2 == 0)
s = -1;
pi = pi + s / (2 * k - 1);
}
return PyFloat_FromDouble(4 * pi);
};
static PyMethodDef AddMethods[] = {
{"myPi", Pi, METH_VARARGS, "Compute Pi"},
{NULL, NULL, 0, NULL} // sentinel
};
static struct PyModuleDef addmodule = {
PyModuleDef_HEAD_INIT,
"Pi",
"C library to compute Pi",
-1,
AddMethods};
PyMODINIT_FUNC PyInit_Pi(void)
{
return PyModule_Create(&addmodule);
};
int main(int argc, char *argv[])
{
PyImport_AppendInittab("Pi", &PyInit_Pi);
Py_Initialize();
PyRun_SimpleString("import Pi\n"
"print(Pi.myPi(1,1000))\n");
Py_FinalizeEx();
return 0;
}
Page 248
#define PY_SSIZE_T_CLEAN
#include <Python.h>
int main(int argc, char *argv[])
{
Py_Initialize();
PyObject *math =PyImport_ImportModule("math");
PyObject *mathdict = PyModule_GetDict(math);
PyObject *myFunction = PyDict_GetItemString(mathdict, "sqrt");
PyObject *args = Py_BuildValue("(I)", 2);
PyObject *result = PyObject_Call(myFunction, args, NULL);
double res=PyFloat_AS_DOUBLE(result);
printf("%f\n",res);
PyRun_SimpleString(
"import math\n"
"print(math.sqrt(2))\n");
Py_FinalizeEx();
return 0;
}
Page 249
#define PY_SSIZE_T_CLEAN
#include <Python.h>
int main(int argc, char *argv[])
{
Py_Initialize();
{
PyObject *g = PyDict_New();
PyObject *l = PyDict_New();
PyDict_SetItemString(l, "X", PyLong_FromLong(2));
PyDict_SetItemString(l, "Y", PyLong_FromLong(2));
PyObject *pyResult = PyRun_String("X+Y", Py_eval_input, g, l);
int Cresult = PyLong_AS_LONG(pyResult);
printf("%d\n", Cresult);
}
{
PyObject *g = PyDict_New();
PyObject *l = PyDict_New();
PyObject *math = PyImport_ImportModule("math");
PyDict_SetItemString(g, "math", math);
PyObject *pyResult = PyRun_String("math.sqrt(2)", Py_eval_input, g, l);
double res2 = PyFloat_AS_DOUBLE(pyResult);
printf("%f\n", res2);
}
Py_FinalizeEx();
return 0;
}
Page 250
#define PY_SSIZE_T_CLEAN
#include <Python.h>
static PyObject *Pi(PyObject *self, PyObject *args)
{
int m, n;
double pi, s;
if (!PyArg_ParseTuple(args, "ii", &m, &n))
return NULL;
pi = 0;
for (int k = m; k < n; k++)
{
s = 1;
if (k % 2 == 0)
s = -1;
pi = pi + s / (2 * k - 1);
}
return PyFloat_FromDouble(4 * pi);
}
static PyMethodDef AddMethods[] = {
{"myPi", Pi, METH_VARARGS, "Compute Pi"},
{NULL, NULL, 0, NULL} // sentinel
};
static struct PyModuleDef addmodule = {
PyModuleDef_HEAD_INIT,
"Pi",
"C library to compute Pi",
-1,
AddMethods};
PyMODINIT_FUNC PyInit_Pi(void)
{
return PyModule_Create(&addmodule);
}
int main(int argc, char *argv[])
{
PyImport_AppendInittab("Pi", &PyInit_Pi);
Py_Initialize();
PyObject *main = PyImport_AddModule("__main__");
PyObject *mainDict = PyModule_GetDict(main);
FILE *fp = fopen("Pi.py", "r");
PyObject *l = PyDict_New();
PyObject *result = PyRun_File(fp, "Pi.py", Py_file_input, mainDict, l);
Py_FinalizeEx();
return 0;
}
Pi,py
import Pi
import time
N=10000000
t1=time.perf_counter()
pi=Pi.myPi(1,N)
t2=time.perf_counter()
print((t2-t1)*1000)
print(pi)
- << Prev
- Next