优化这段代码 sprintf(szVal, "%0.3f" , get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])); LCD_DisString((i%LISTOFFSET)+1, 19, szVal); if (DBData[i] >= RT1064_YS_GL1 && DBData[i] <= RT1064_YS_FBS_JY) LCD_DisString((i%9)+1, 25, (char *)"S"); else if ((DBData[i] >= RT1064_DZ_GL1 && DBData[i] <= RT1064_DZ_I02) || ((DBData[i] >= RT1064_DZ_PHASE_I && DBData[i] <= RT1064_DZ_I0DLT) && DBData[i] != RT1064_DZ_YL_HAR && DBData[i] != RT1064_DZ_LMJ) || DBData[i] == RT1064_DZ_SD || DBData[i] == RT1064_DZ_I0HJS || DBData[i] == RT1064_DZ_FC_CHZ || (DBData[i] >= RT1064_DZ_I03 && DBData[i] <= RT1064_DZ_FBS_I0)|| DBData[i] == RT1064_DZ_FBS_OL) { LCD_DisString((i%LISTOFFSET)+1, 25, (char *)"A"); } else if (DBData[i] == RT1064_DZ_LMJ || DBData[i] == RT1064_DZ_JC || DBData[i] == RT1064_DZ_GYJC) { LCD_DisString((i%LISTOFFSET)+1, 25, (char *)"°"); } else if (DBData[i] == RT1064_DZ_YL_HAR) LCD_DisString((i%LISTOFFSET)+1, 25, (char *)"%"); else if (DBData[i] == RT1064_DZ_IDS ||DBData[i] == RT1064_DZ_OPENCS || (DBData[i] >= RT1064_DZ_I03 && DBData[i] <= RT1064_DZ_FBS_I0)) { LCD_DisString((i%LISTOFFSET)+1, 24, (char *)"次"); } else if (DBData[i] == RT1064_DZ_DP || DBData[i] == RT1064_DZ_GP) { LCD_DisString((i%LISTOFFSET)+1, 25, (char *)"HZ"); } else LCD_DisString((i%LISTOFFSET)+1, 25, (char *)"V"); if (isDraw == 1)len = LCD_BitNot(UNIT_PROTECT,UNIT_GAP_RT1064,gapid,DBData[i],i,bit);
时间: 2024-04-28 11:20:19 浏览: 9
针对这段代码,可以考虑以下几个优化方案:
1. 减少字符串拷贝。在sprintf()和LCD_DisString()函数中,传入的字符串会被拷贝到字符串缓存或屏幕缓存中,如果频繁调用这些函数,会导致不必要的字符串拷贝开销。可以考虑将字符串缓存起来,只在需要更新时才更新。
2. 将条件判断合并。在判断需要显示的字符时,使用了多个if语句,可以将这些条件合并成一个switch语句,减少判断次数。
3. 避免重复计算。在调用LCD_DisString()函数时,计算了(i%LISTOFFSET)+1多次,可以将其缓存到一个变量中,减少重复计算的开销。
4. 减少函数调用。在更新屏幕信息时,调用了多个函数,如果频繁调用这些函数,会导致不必要的函数调用开销。可以考虑将这些函数合并成一个函数,减少函数调用的次数。
综上所述,优化这段代码的关键是减少字符串拷贝和函数调用,以及合并条件判断和缓存重复计算结果。
相关问题
优化这段代码 if( DBData[i] >= RT1064KZZ_GL1_ALM && DBData[i] <= RT1064KZZ_KZHL && DBData[i] != RT1064KZZ_MODE && DBData[i] != RT1064KZZ_UAB_CH && DBData[i] != RT1064KZZ_UBC_CH && DBData[i] != RT1064KZZ_FBS && DBData[i] != RT1064KZZ_FBS_MODE) { LCD_DisString((i%9)+1, 20, (char *)gcszOnOff[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]); if (not == 2) { if(draw == (i+1)) { LCD_DisString_Not((i%9)+1, 20, (char *)gcszOnOff[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]); } } } else if(DBData[i] == RT1064KZZ_MODE) { LCD_DisString((i%9)+1, 20, (char *)ModeName[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]); if (not == 2) { if(draw == (i+1)) { LCD_DisString_Not((i%9)+1, 20, (char *)ModeName[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]); } } } else if (DBData[i] == RT1064KZZ_FBS || DBData[i] == RT1064KZZ_FBS_MODE ) { if (DBData[i] == RT1064KZZ_FBS) { LCD_DisString((i%9)+1, 20, (char *)FBS_NAME[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]); if (not == 2) { if(draw == (i+1)) { LCD_DisString_Not((i%9)+1, 20, (char *)FBS_NAME[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]); } } } else if(DBData[i] == RT1064KZZ_FBS_MODE ) { LCD_DisString((i%9)+1, 20, (char *)FBS_MODE_NAME[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]); if (not == 2) { if(draw == (i+1)) { LCD_DisString_Not((i%9)+1, 20, (char *)FBS_MODE_NAME[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]); } } } } else if(DBData[i] == RT1064KZZ_UAB_CH || DBData[i] == RT1064KZZ_UBC_CH || DBData[i] == RT1064_DZ_CHZCS) { sprintf(szVal, "%0.f" , get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])); LCD_DisString((i%9)+1, 19 , szVal); if(DBData[i] == RT1064_DZ_CHZCS) LCD_DisString((i%9)+1, 24, (char *)"次"); if (not == 2) { if(draw == (i+1)) { len = LCD_BitNot(UNIT_PROTECT,UNIT_GAP_RT1064,gapid,DBData[i],i,bit); } } }
可以尝试使用 switch-case 语句替代多个 if-else,代码会更加简洁易懂。同时,对于重复的代码可以将其提取出来,避免代码冗余。例如,LCD_DisString 和 LCD_DisString_Not 的代码可以提取出来,避免重复编写。最后,可以考虑使用注释来解释代码的作用,增强代码可读性。以下是修改后的代码示例:
```
for(int i=0;i<RT1064KZZ_DZS;i++)
{
switch(DBData[i])
{
case RT1064KZZ_MODE:
LCD_DisString((i%9)+1, 20, (char *)ModeName[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]);
if(not == 2 && draw == (i+1))
{
LCD_DisString_Not((i%9)+1, 20, (char *)ModeName[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]);
}
break;
case RT1064KZZ_FBS:
LCD_DisString((i%9)+1, 20, (char *)FBS_NAME[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]);
if(not == 2 && draw == (i+1))
{
LCD_DisString_Not((i%9)+1, 20, (char *)FBS_NAME[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]);
}
break;
case RT1064KZZ_FBS_MODE:
LCD_DisString((i%9)+1, 20, (char *)FBS_MODE_NAME[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]);
if(not == 2 && draw == (i+1))
{
LCD_DisString_Not((i%9)+1, 20, (char *)FBS_MODE_NAME[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]);
}
break;
case RT1064KZZ_UAB_CH:
case RT1064KZZ_UBC_CH:
case RT1064_DZ_CHZCS:
sprintf(szVal, "%0.f" , get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i]));
LCD_DisString((i%9)+1, 19 , szVal);
if(DBData[i] == RT1064_DZ_CHZCS) LCD_DisString((i%9)+1, 24, (char *)"次");
if(not == 2 && draw == (i+1))
{
len = LCD_BitNot(UNIT_PROTECT,UNIT_GAP_RT1064,gapid,DBData[i],i,bit);
}
break;
default:
if(DBData[i] >= RT1064KZZ_GL1_ALM && DBData[i] <= RT1064KZZ_KZHL && DBData[i] != RT1064KZZ_MODE && DBData[i] != RT1064KZZ_UAB_CH && DBData[i] != RT1064KZZ_UBC_CH && DBData[i] != RT1064KZZ_FBS && DBData[i] != RT1064KZZ_FBS_MODE)
{
LCD_DisString((i%9)+1, 20, (char *)gcszOnOff[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]);
if(not == 2 && draw == (i+1))
{
LCD_DisString_Not((i%9)+1, 20, (char *)gcszOnOff[(int)get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])]);
}
}
break;
}
}
```
优化这段代码 sprintf(szVal, "%0.3f" , get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])); LCD_DisString((i%9)+1, 19, szVal); if (DBData[i] >= RT1064_YS_GL1 && DBData[i] <= RT1064_YS_FBS_JY) LCD_DisString((i%9)+1, 25, (char *)"S"); else if ((DBData[i] >= RT1064_DZ_GL1 && DBData[i] <= RT1064_DZ_I02) || ((DBData[i] >= RT1064_DZ_PHASE_I && DBData[i] <= RT1064_DZ_I0DLT) && DBData[i] != RT1064_DZ_YL_HAR && DBData[i] != RT1064_DZ_LMJ) || DBData[i] == RT1064_DZ_SD || DBData[i] == RT1064_DZ_I0HJS || DBData[i] == RT1064_DZ_FC_CHZ || (DBData[i] >= RT1064_DZ_I03 && DBData[i] <= RT1064_DZ_FBS_I0)|| DBData[i] == RT1064_DZ_FBS_OL) { LCD_DisString((i%9)+1, 25, (char *)"A"); } else if (DBData[i] == RT1064_DZ_LMJ || DBData[i] == RT1064_DZ_JC || DBData[i] == RT1064_DZ_GYJC) { LCD_DisString((i%9)+1, 25, (char *)"°"); } else if (DBData[i] == RT1064_DZ_YL_HAR) LCD_DisString((i%9)+1, 25, (char *)"%"); else if (DBData[i] == RT1064_DZ_IDS ||DBData[i] == RT1064_DZ_OPENCS || (DBData[i] >= RT1064_DZ_I03 && DBData[i] <= RT1064_DZ_FBS_I0)) { LCD_DisString((i%9)+1, 24, (char *)"次"); } else if (DBData[i] == RT1064_DZ_DP || DBData[i] == RT1064_DZ_GP) { LCD_DisString((i%9)+1, 25, (char *)"HZ"); } else LCD_DisString((i%9)+1, 25, (char *)"V"); if (not == 2) { if(draw == (i+1)) len = LCD_BitNot(UNIT_PROTECT,UNIT_GAP_RT1064,gapid,DBData[i],i,bit); }
这段代码可以进行如下优化:
1. 将重复出现的 RT1064_DZ_xxx 值定义为常量,以提高代码可读性。
2. 将 if-else 语句转换为 switch-case 语句,以简化代码。
3. 将重复出现的 (i%9)+1 计算结果赋值给变量,以提高代码可读性。
以下是优化后的代码:
```c
const int RT1064_YS_GL1 = 1;
const int RT1064_YS_FBS_JY = 2;
const int RT1064_DZ_GL1 = 3;
// ... 其他 RT1064_DZ_xxx 常量
int iMod9 = i % 9 + 1;
char szVal[8];
sprintf(szVal, "%0.3f", get_ActionDZInfo_val(UNIT_GAP_RT1064, gapid, DBData[i]));
LCD_DisString(iMod9, 19, szVal);
switch (DBData[i]) {
case RT1064_DZ_LMJ:
case RT1064_DZ_JC:
case RT1064_DZ_GYJC:
LCD_DisString(iMod9, 25, (char *) "°");
break;
case RT1064_DZ_YL_HAR:
LCD_DisString(iMod9, 25, (char *) "%");
break;
case RT1064_DZ_IDS:
case RT1064_DZ_OPENCS:
case RT1064_DZ_I03:
case RT1064_DZ_I04:
case RT1064_DZ_I05:
case RT1064_DZ_I06:
case RT1064_DZ_I07:
case RT1064_DZ_I08:
case RT1064_DZ_I09:
case RT1064_DZ_I10:
case RT1064_DZ_I11:
case RT1064_DZ_FBS_I0:
LCD_DisString(iMod9, 24, (char *) "次");
break;
case RT1064_DZ_DP:
case RT1064_DZ_GP:
LCD_DisString(iMod9, 25, (char *) "HZ");
break;
default:
LCD_DisString(iMod9, 25, (char *) "V");
if (not == 2 && draw == i + 1) {
len = LCD_BitNot(UNIT_PROTECT, UNIT_GAP_RT1064, gapid, DBData[i], i, bit);
}
break;
}
if (DBData[i] >= RT1064_YS_GL1 && DBData[i] <= RT1064_YS_FBS_JY) {
LCD_DisString(iMod9, 25, (char *) "S");
}
else if ((DBData[i] >= RT1064_DZ_GL1 && DBData[i] <= RT1064_DZ_I02) ||
((DBData[i] >= RT1064_DZ_PHASE_I && DBData[i] <= RT1064_DZ_I0DLT) &&
DBData[i] != RT1064_DZ_YL_HAR && DBData[i] != RT1064_DZ_LMJ) ||
DBData[i] == RT1064_DZ_SD || DBData[i] == RT1064_DZ_I0HJS ||
DBData[i] == RT1064_DZ_FC_CHZ || (DBData[i] >= RT1064_DZ_I03 && DBData[i] <= RT1064_DZ_FBS_I0) ||
DBData[i] == RT1064_DZ_FBS_OL) {
LCD_DisString(iMod9, 25, (char *) "A");
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)