优化代码void can_app_sig_rx_vehicle_speed_polling(void) { U16 speed = 0; U8 speed_ret = can_app_il_get_rx_ESP_v_Signal_1(&speed); U8 speed_valid=0; can_app_il_get_rx_ESP_QBit_v_Signal(&speed_valid); U8 speed_timeout = ((speed_ret&CAN_IL_TIMEOUT)!=CAN_IL_TIMEOUT)?FALSE:TRUE; static U8 speed_timeout_bk = 0; static BOOL speed_update_flag = FALSE; if(speed_timeout_bk != speed_timeout) { if(speed_timeout) { can_sig_vehicle_speed_valid = 1; can_app_output_vehicle_speed(); can_sig_vehicle_speed_valid = speed_valid; } else//when timeout change to normal,need send it. { speed_update_flag = TRUE; } speed_timeout_bk = speed_timeout; } if((can_sig_vehicle_speed != speed)||(can_sig_vehicle_speed_valid != speed_valid)||speed_update_flag) { can_sig_vehicle_speed = speed; can_sig_vehicle_speed_valid = speed_valid; speed_update_flag = FALSE; can_app_output_vehicle_speed(); } }
时间: 2023-08-08 15:12:28 浏览: 99
优化YOLOv8代码得到的
可以尝试使用以下优化方式:
1. 减少函数调用次数。可以将 `can_app_il_get_rx_ESP_v_Signal_1` 和 `can_app_il_get_rx_ESP_QBit_v_Signal` 的返回值存储在局部变量中,避免多次调用。
2. 避免过多的变量复制。可以直接将 `speed_ret&CAN_IL_TIMEOUT` 的结果赋值给 `speed_timeout`,避免中间变量的使用。
3. 尽可能减少全局变量的使用。可以将 `can_sig_vehicle_speed` 和 `can_sig_vehicle_speed_valid` 作为参数传递给 `can_app_output_vehicle_speed` 函数,避免全局变量的使用。
4. 将静态变量的初始化移动到函数外部,避免每次函数调用都进行初始化。
下面是优化后的代码示例:
```c
void can_app_sig_rx_vehicle_speed_polling(U16 *vehicle_speed, U8 *vehicle_speed_valid) {
U16 speed = 0;
U8 speed_ret = 0;
U8 speed_valid = 0;
static U8 speed_timeout_bk = 0;
static BOOL speed_update_flag = FALSE;
static BOOL speed_timeout = FALSE;
speed_ret = can_app_il_get_rx_ESP_v_Signal_1(&speed);
can_app_il_get_rx_ESP_QBit_v_Signal(&speed_valid);
speed_timeout = (speed_ret & CAN_IL_TIMEOUT) ? TRUE : FALSE;
if (speed_timeout_bk != speed_timeout) {
if (speed_timeout) {
*vehicle_speed_valid = 1;
speed_update_flag = TRUE;
}
speed_timeout_bk = speed_timeout;
}
if (*vehicle_speed != speed || *vehicle_speed_valid != speed_valid || speed_update_flag) {
*vehicle_speed = speed;
*vehicle_speed_valid = speed_valid;
can_app_output_vehicle_speed(*vehicle_speed, *vehicle_speed_valid);
speed_update_flag = FALSE;
}
}
```
阅读全文