· Best loop for a timeout: 295000 cycles for 20000 iterations.
//we want to execute do_func() approx. 20000 times before timing out
timeout=(20000/0x100)*0x100; //keeps lobyte(timeout)==0, which speeds up assignments
//set up tmr0 to set flag T0IF high when it rolls over
while(RA0==0 && !T0IF); //wait until port goes high
结论4:
·尽可能地使用内建的定时器及中断标志。
优化提示5: Case 语句
慢而且效率低
c=getch();
switch(c)
{
case 'A':
{
do something;
break;
}
case 'H':
{
do something;
break;
}
case 'Z':
{
do something;
break;
}
}
getch 快且高效率
c=getch();
switch(c)
{
case 0:
{
do something;
break;
}
case 1:
{
do something;
break;
}
case 2:
{
do something;
break;
}
}
Hi-Tech C的优化器会尽可能地把switch语句变成计算偏移的goto作者: admin 时间: 2009-4-19 01:26
结论5:
·尽可能地在case语句里使用连续的数字。
优化提示6: Hi-Tech C里的除法
如果你使用Hi-Tech C,在你程序的任何位置有任何数学除法的运算,就将会使用到bank0里13到23个字节的空间,以及一些EPROM/Flash程序空间。
尽管变量不在bank0,这一样会发生。
Occurrence
Any mathematical division at all in the entire program using a variable of type 'long', even if all variables do not reside in bank0.
RAM usage
23 bytes in bank0
ROM/flash usage
large, it has to include ldiv routines
Fix/Explanation
Use combinations of bit shifts ie: x=x*6 is replaced by x1=x;x2=x;x=x1<<2 + x2<<1
Occurrence
Any mathematical division at all in the entire program using a variable of type 'unsigned int', even if all variables do not reside in bank0.
RAM usage
13 bytes in bank0
ROM/flash usage
large,it has to include ldiv routines
Fix/Explanation
Use combinations of bit shifts
Occurrence
Any mathematical division involving a divisor that is a power of 2, ie: x=x/64;
RAM usage
none
ROM/flash usage
low
Fix/Explanation
Use combinations of bit shifts
Occurrence
Any mathematical division involving a divisor that is not a power of 2, ie: x=x/65;
RAM usage
none
ROM/flash usage
high
Fix/Explanation
make your divisors a power of 2, ie: 2^5=32.作者: admin 时间: 2009-4-19 01:27
结论6:
如果可能,尽量让C编译器使用简单的移位来做除法,且除数是2的幂。除数是2的幂,例如,256=2^8,可以被C编译器优化为移位操作。