替代大量的if语句?

时间:2019-02-09 05:24:20

标签: c if-statement embedded stm32 stm32f4

我有一组If语句(很多,大约40个),它们每个都检查嵌入式板的输入。由于某种原因,运行这组语句确实很慢。

uint32_t INPUT_ARRAY[40];
#define INPUT_X INPUT_ARRAY[0] // input 1 corresponds to the first array slot and so on, easy to call up a specific input.

void main(){
    while(1) // infinite loop for embedded program
    { 
        Read_Inputs(); // input read function

        Write_Outputs(); // output write function

        Logic_Test(); // This is to test out the inputs and outputs on our hardware test rig
    }
}

inline void Logic_Test(void){
    if ( INPUT_1 != 0){
       output_3 |= some_bit // this logic could change
       output_10 |= another_bit 
    }
    if ( INPUT_2 != 0){
       output_x |= some_bit 
    }
    if ( INPUT_3 != 0){
       output_x |= some_bit 
    }
    if ( INPUT_4 != 0){
       output_x |= some_bit // this logic could change
    }
    if ( INPUT_X != 0){
       output_x |= some_bit // this logic could change
    }
    if ( INPUT_X != 0){
       output_x |= some_bit // this logic could change
    }
    if ( INPUT_X != 0){
       output_x |= some_bit // this logic could change
    }
    if ( INPUT_X != 0){
       output_x |= some_bit // this logic could change
    }
    if ( INPUT_X != 0){
       output_x |= some_bit // this logic could change
    }
    if ( INPUT_X != 0){
       output_x |= some_bit // this logic could change
    }
    if ( INPUT_X != 0){
       output_x |= some_bit // this logic could change
    }
    if ( INPUT_X != 0){
       output_x |= some_bit // this logic could change
    }
    if ( INPUT_X != 0){
       output_x |= some_bit // this logic could change
    }
    if ( INPUT_X != 0){
       output_x |= some_bit // this logic could change
    }
    if ( INPUT_X != 0){
       output_x |= some_bit // this logic could change
    }
    if ( INPUT_X != 0){
       output_x |= some_bit // this logic could change
    }
    if ( INPUT_40 != 0){
       output_x |= some_bit 
    }
}

代码的结构如上,从1到40。它们不是if-else-if语句。我会使用一个开关,但是据我所知,一个开关一次只能覆盖一种情况,并且可以打开多个输入。是否有一种更快的方式来覆盖所有输入而无需使用太多ifs?

其他信息: 我们正在使用主频为180 Mhz的STM32F4系列(F429ZI)。

3 个答案:

答案 0 :(得分:3)

如果分支影响性能,则可以删除分支。例如,如果您要设置一个位,而不是说:

function amp_comment_submit(){
  $comment = wp_handle_comment_submission( wp_unslash( $_POST ) );
  if ( is_wp_error( $comment ) ) {
    $data = intval( $comment->get_error_data() );
    if ( ! empty( $data ) ) {
      status_header(500);
      wp_send_json(array('msg' => $comment->get_error_message(),
                        'response' => $data, 
                        'back_link' => true ));
    }
  }
  else {
    @header('AMP-Redirect-To: '. get_permalink($_POST['comment_post_ID']));
    @header('AMP-Access-Control-Allow-Source-Origin: ' . $_REQUEST['__amp_source_origin'] );
    wp_send_json(array('success' => true));
  }
}
add_action('wp_ajax_amp_comment_submit', 'amp_comment_submit');
add_action('wp_ajax_nopriv_amp_comment_submit', 'amp_comment_submit');

您可以无条件地这样做:

if (INPUT_n != 0) {
   output_x |= (1 << 15);
}

如果您为每个输入设置 same 位,则它可能会折叠为:

output_x |= ((INPUT_n != 0) << 15);

答案 1 :(得分:2)

使用循环。您可以根据需要使用 for 循环, while 循环或 do while 循环。 for 循环更为常见。

int i; 
for(i=1;i<=Number_Of_times_following_code_need_to_run;i++)
{
   \\Your_desired_code_here
}

添加更多当前代码结构后:-

  for(i=0;i<40;i++) \\40_is_your_array_length_by_your_code
   {
     if(INPUT_ARRAY[i] != 0){
       output_x |= some_bit
      }
   }

答案 2 :(得分:0)

使用64位掩码而不是uint32_t INPUT_ARRAY[40];

肯定地,代码使用索引数组来方便设置/清除。取而代之的是使用位掩码来提高 test 的性能,这种情况发生的次数肯定比设置/清除的次数更多。