作者bbnctu ()
看板C_and_CPP
標題[問題] 請教關於執行效率
時間Fri Apr 3 14:05:00 2015
開發平台(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++;
}
感謝感謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.143.71.224
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1428041102.A.E53.html
推 LiloHuang: 上述兩種方法,開啟編譯器最佳化後,兩者會是相同的。 04/03 14:48
→ LiloHuang: 在 Release 模式下,兩種方法所產生的 asm 指令會相同 04/03 14:51
→ LiloHuang: 如果生成出的指令都一致,兩者執行效率就會相同 :) 04/03 14:54
→ dirkc: isr如果會存取aaa_db的話,我想你考慮過race condition了吧 04/04 00:24
→ alongalone: 第一種寫法是避免re-entrant 的時候炸掉 04/04 09:15
→ alongalone: 啊, 有點看錯. 看到取值就以為是把struct傳進來..Orz 04/04 09:16