評價: 0 回應: 1 閱覽: 91
置頂

關於執行效率

typedef struct _AAA{

   int a;
   int b;
   int c;

}AAA_T;

AAA_T aaa_db[10];

想請教板上各位先進

在function裡面
對於global structure以下兩種access aaa_db的方式
在執行效率是否有什麼不同?
如果考慮執行過程中有可能會被更高priority的ISR插斷
是否有哪一種做法較好呢?

Method 1 :

void test1(uint8 idx)
{
   AAA_T *aaa_db_p = &aaa_db[idx];
   aaa_db_p->a++;
   aaa_db_p->b++;
   aaa_db_p->c++;
}


Method 2 :

void test2(uint8 idx)
{

   aaa_db[idx].a++;
   aaa_db[idx].b++;
   aaa_db[idx].c++;
}


感謝

熱門回應

上述兩種方法,開啟編譯器最佳化後,兩者會是相同的。

可打開 /FAs 選項  觀察 cod 檔案

在 Release 模式下,兩種方法所產生的 asm 指令會相同

如果生成出的指令都一致,兩者執行效率就會相同

會員登入 (先登入會員才能回覆留言喔!)

Facebook留言