1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| #include <python3.6/Python.h> #include <iostream> #include <vector> #include <ctime>
using namespace std;
int main() { float result_sin, result_c; float t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12;
std::vector<float> x_sin = { -1.04719755, -0.97239773, -0.8975979, -0.82279808, -0.74799825, -0.67319843, -0.5983986, -0.52359878, -0.44879895, -0.37399913, -0.2991993, -0.22439948, -0.14959965, -0.07479983, 0. };
std::vector<float> y_sin = { -8.69493254, -7.80773922, -8.89057923, -8.17521393, -7.46637712, -5.61835116, -7.03078625, -3.74118736, -2.80825583, -3.18352721, -0.71848767, 1.24557015, 2.28072893, 3.06334741, 3.44595441 };
std::vector<float> x_c = { -6.28318531, -5.38558741, -4.48798951, -3.5903916, -2.6927937, -1.7951958, -0.8975979, 0., 0.8975979, 1.7951958, 2.6927937, 3.5903916, 4.48798951, 5.38558741, 6.28318531 };
std::vector<float> y_c = { 2.86945587, 1.84842162, 2.5238136, 3.08299978, 2.44221503, 2.3558647, 3.58941682, 3.35658766, 3.44169236, 2.92365404, 3.38294647, 3.35855088, 2.71984826, 3.06090843, 2.49285146 };
t1 = clock(); Py_Initialize(); t2 = clock();
if(Py_IsInitialized()) { t3 = clock(); PyObject* pModule = PyImport_ImportModule("script"); t4 = clock();
t5 = clock(); PyObject *pFunc_fit_sin = PyObject_GetAttrString(pModule, "fit_sin"); PyObject *pFunc_func_sin = PyObject_GetAttrString(pModule, "func_sin"); PyObject *pFunc_error_sin = PyObject_GetAttrString(pModule, "error_sin");
PyObject* pArgs_sin = PyTuple_New(5); PyObject* pRslt_sin = nullptr;
PyObject* pArg_x_sin = PyList_New(15); PyObject* pArg_y_sin = PyList_New(15); PyObject* pArg_p_sin = PyList_New(4); for(int i = 0; i < 15; ++i) {PyList_SetItem(pArg_x_sin, i, Py_BuildValue("f", x_sin[i]));} for(int i = 0; i < 15; ++i) {PyList_SetItem(pArg_y_sin, i, Py_BuildValue("f", y_sin[i]));} PyList_SetItem(pArg_p_sin, 0, Py_BuildValue("f", 7)); PyList_SetItem(pArg_p_sin, 1, Py_BuildValue("f", 0.1)); PyList_SetItem(pArg_p_sin, 2, Py_BuildValue("f", 0)); PyList_SetItem(pArg_p_sin, 3, Py_BuildValue("f", 0));
PyTuple_SetItem(pArgs_sin, 0, pArg_x_sin); PyTuple_SetItem(pArgs_sin, 1, pArg_y_sin); PyTuple_SetItem(pArgs_sin, 2, pArg_p_sin); PyTuple_SetItem(pArgs_sin, 3, Py_BuildValue("f", 0.7)); PyTuple_SetItem(pArgs_sin, 4, Py_BuildValue("i", 1000));
if(pModule && pFunc_fit_sin && pFunc_func_sin && pFunc_error_sin) { pRslt_sin = PyEval_CallObject(pFunc_fit_sin, pArgs_sin); PyArg_Parse(pRslt_sin, "f", &result_sin); std::cout << "正弦拟合积分结果: " << result_sin << std::endl; } else { cout << "Failed to load pModule or pFuncs_sin." << endl; } t7 = clock();
t8 = clock(); PyObject *pFunc_fit_c = PyObject_GetAttrString(pModule, "fit_c"); PyObject *pFunc_func_c = PyObject_GetAttrString(pModule, "func_c"); PyObject *pFunc_error_c = PyObject_GetAttrString(pModule, "error_c");
PyObject* pArgs_c = PyTuple_New(5); PyObject* pRslt_c = nullptr;
PyObject* pArg_x_c = PyList_New(15); PyObject* pArg_y_c = PyList_New(15); for(int i = 0; i < 15; ++i) {PyList_SetItem(pArg_x_c, i, Py_BuildValue("f", x_c[i]));} for(int i = 0; i < 15; ++i) {PyList_SetItem(pArg_y_c, i, Py_BuildValue("f", y_c[i]));}
PyTuple_SetItem(pArgs_c, 0, pArg_x_c); PyTuple_SetItem(pArgs_c, 1, pArg_y_c); PyTuple_SetItem(pArgs_c, 2, Py_BuildValue("f", 1)); PyTuple_SetItem(pArgs_c, 3, Py_BuildValue("f", 0.7)); PyTuple_SetItem(pArgs_c, 4, Py_BuildValue("i", 200));
if(pModule && pFunc_fit_c && pFunc_func_c && pFunc_error_c) { pRslt_c = PyEval_CallObject(pFunc_fit_c, pArgs_c); PyArg_Parse(pRslt_c, "f", &result_c); std::cout << "直线拟合积分结果: " << result_c << std::endl; } else { cout << "Failed to load pModule or pFuncs_c." << endl; } t10 = clock(); } else { cout << "Python initialization failed." << endl; }
t11 = clock(); Py_Finalize(); t12 = clock();
cout << "Py环境初始化耗时: " << (t2 - t1)/CLOCKS_PER_SEC *1000 << "ms" << endl; cout << "载入脚本耗时: " << (t4 - t3)/CLOCKS_PER_SEC *1000 << "ms" << endl; cout << "正弦拟合过程耗时: " << (t7 - t5)/CLOCKS_PER_SEC *1000 << "ms" << endl; cout << "直线拟合过程耗时: " << (t10 - t8)/CLOCKS_PER_SEC *1000 << "ms" << endl; cout << "结束Py环境耗时: " << (t12 - t11)/CLOCKS_PER_SEC *1000 << "ms" << endl; }
|