我需要一些帮助来提高我的代码效率。代码行做我想做的事,但做得不好。我需要它更高效,因为我稍后会增加数据量,然后功能会太慢。
我也是 C++ 和编程的新手,因此欢迎任何建议,以便我了解更多信息。
因为大多数时候更改很少,我只希望函数 B 进一步发送函数 A 中更改的值。现在在我的代码中,即使根本没有更改,每个值也会进一步发送。我知道,这是对资源的浪费和不好的编程。
重要的!1)IOCP.sendVarData的第一个参数的值是静态的,不能改变。2) 函数 B 中的 PmdgGetVariable ("MCP_annunFD", &result) 启动一切。改善这一点的最佳方法是什么?
我有以下功能,可以检测上次运行时的任何更改并存储更改。
// NGX数据变化时调用该函数 void ProcessNGXData (PMDG_NGX_Data *pS) {
if (pS->MCP_Course[0] != NGX_MCP_Course[0]){NGX_MCP_Course[0] = pS->MCP_Course[0];}
if (pS->MCP_Course[1] != NGX_MCP_Course[1]){NGX_MCP_Course[1] = pS->MCP_Course[1];}
if (pS->MCP_IASMach != NGX_MCP_IASMach){NGX_MCP_IASMach = pS->MCP_IASMach;}
if (pS->MCP_Heading != NGX_MCP_Heading){NGX_MCP_Heading = pS->MCP_Heading;}
if (pS->MCP_Altitude != NGX_MCP_Altitude){NGX_MCP_Altitude = pS->MCP_Altitude;}
if (pS->MCP_VertSpeed != NGX_MCP_VertSpeed){NGX_MCP_VertSpeed = pS->MCP_VertSpeed;}
if (pS->MCP_annunLVL_CHG != NGX_MCP_annunLVL_CHG){NGX_MCP_annunLVL_CHG = pS->MCP_annunLVL_CHG;}
if (pS->MCP_annunN1 != NGX_MCP_annunN1) {NGX_MCP_annunN1 = pS->MCP_annunN1;}
if (pS->MCP_annunSPEED != NGX_MCP_annunSPEED) {NGX_MCP_annunSPEED = pS->MCP_annunSPEED;}
if (pS->MCP_annunVNAV != NGX_MCP_annunVNAV) {NGX_MCP_annunVNAV = pS->MCP_annunVNAV;}
if (pS->MCP_annunLVL_CHG != NGX_MCP_annunLVL_CHG){NGX_MCP_annunLVL_CHG = pS->MCP_annunLVL_CHG;}
if (pS->MCP_annunHDG_SEL != NGX_MCP_annunHDG_SEL){NGX_MCP_annunHDG_SEL = pS->MCP_annunHDG_SEL;}
if (pS->MCP_annunLNAV != NGX_MCP_annunLNAV) {NGX_MCP_annunLNAV = pS->MCP_annunLNAV;}
if (pS->MCP_annunVOR_LOC != NGX_MCP_annunVOR_LOC){NGX_MCP_annunVOR_LOC = pS->MCP_annunVOR_LOC ;}
if (pS->MCP_annunAPP != NGX_MCP_annunAPP ) {NGX_MCP_annunAPP = pS->MCP_annunAPP ;}
if (pS->MCP_annunALT_HOLD != NGX_MCP_annunALT_HOLD){NGX_MCP_annunALT_HOLD = pS->MCP_annunALT_HOLD ;}
if (pS->MCP_annunVS != NGX_MCP_annunVS ) {NGX_MCP_annunVS = pS->MCP_annunVS ;}
if (pS->MCP_annunCMD_A != NGX_MCP_annunCMD_A ) {NGX_MCP_annunCMD_A = pS->MCP_annunCMD_A ;}
if (pS->MCP_annunCWS_A != NGX_MCP_annunCWS_A ) {NGX_MCP_annunCWS_A = pS->MCP_annunCWS_A ;}
if (pS->MCP_annunCMD_B != NGX_MCP_annunCMD_B ) {NGX_MCP_annunCMD_B = pS->MCP_annunCMD_B ;}
if (pS->MCP_annunCWS_B != NGX_MCP_annunCWS_B) {NGX_MCP_annunCWS_B = pS->MCP_annunCWS_B ;}
}
在另一个线程中,我现在有这个函数来发送
static UINT running_as_Thread(LPVOID lpvoid) {
int result;
bool rep= true;
while (rep){
PmdgGetVariable ("MCP_annunFD", &result);
IOCP.sendVarData ( 2534, NGX_MCP_Course [0]);
IOCP.sendVarData ( 2524, NGX_MCP_Altitude);
IOCP.sendVarData ( 2542, NGX_MCP_Heading);
IOCP.sendVarData ( 2559, NGX_MCP_IASMach);
IOCP.sendVarData ( 1140, NGX_MCP_VertSpeed);
IOCP.sendVarData ( 2535, NGX_MCP_Course [1]);
IOCP.sendVarData ( 286, NGX_MCP_annunLVL_CHG);
IOCP.sendVarData ( 1143, NGX_MCP_VertSpeedBlank);
IOCP.sendVarData ( 1115, NGX_MCP_IASBlank);
IOCP.sendVarData ( 1117 , NGX_MCP_IASOverspeedFlash);
IOCP.sendVarData ( 1118, NGX_MCP_IASUnderspeedFlash);
IOCP.sendVarData ( 282, NGX_MCP_annunATArm);
IOCP.sendVarData ( 283, NGX_MCP_annunN1);
IOCP.sendVarData ( 284, NGX_MCP_annunSPEED);
IOCP.sendVarData ( 285, NGX_MCP_annunVNAV);
IOCP.sendVarData ( 286, NGX_MCP_annunLVL_CHG);
IOCP.sendVarData ( 287, NGX_MCP_annunHDG_SEL);
IOCP.sendVarData ( 288, NGX_MCP_annunLNAV);
IOCP.sendVarData ( 289, NGX_MCP_annunVOR_LOC);
IOCP.sendVarData ( 290, NGX_MCP_annunAPP);
IOCP.sendVarData ( 291, NGX_MCP_annunALT_HOLD);
IOCP.sendVarData ( 292, NGX_MCP_annunVS);
IOCP.sendVarData ( 293, NGX_MCP_annunCMD_A);
IOCP.sendVarData ( 294, NGX_MCP_annunCWS_A);
IOCP.sendVarData ( 295, NGX_MCP_annunCMD_B);
IOCP.sendVarData ( 296, NGX_MCP_annunCWS_B);
Sleep (400);
}
return THREAD_EXIT_OK;
}