[問題] 請教關於執行效率

作者: bbnctu   2015-04-03 14:05:00
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
VC++
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
N/A
問題(Question):
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++;
}
感謝感謝
作者: LiloHuang (十年一刻)   2015-04-03 14:48:00
上述兩種方法,開啟編譯器最佳化後,兩者會是相同的。可打開 /FAs 選項 http://goo.gl/86mCHR 觀察 cod 檔案在 Release 模式下,兩種方法所產生的 asm 指令會相同如果生成出的指令都一致,兩者執行效率就會相同 :)
作者: dirkc (3781615)   2015-04-04 00:24:00
isr如果會存取aaa_db的話,我想你考慮過race condition了吧
作者: alongalone (沿著孤單的路)   2015-04-04 09:15:00
第一種寫法是避免re-entrant 的時候炸掉啊, 有點看錯. 看到取值就以為是把struct傳進來..Orz

Links booklink

Contact Us: admin [ a t ] ucptt.com