On Feb 22, 2010, at 5:20 PM, Jason Hsu wrote: > So incrementing a variable just one time takes 45 clock cycles. I > know that C isn't as efficient as Assembly language, but I didn't > expect it to be this inefficient. First of all, How are you measuring this? Secondly, that's 80 times through your loop in each 45 clocks. The loop includes at least the increment, a compare, and a conditional jump. And function call overhead every millisecond, and two levels of function call every 10ms, etc... Thirdly, at (at least) four cycles per instruction, that's only 11 instructions. While that's certainly not GOOD, it's within the realm of "believable", especially if you're using one of those "free version contains reduced optimization, but which we mean that it's somewhere between "incredibly stupid" and "intentionally awful so you'll buy the real version." (You haven't said which compiler you're using, or what optimization switches you're using, or any of that.) Forth, look at the assembly language produced. Your compiler ought to have an option to produce this, or you can look at it with mplab. For example, using the free version of cc5x, generating code for a 16C54, I see the following when I run your code through it: delay_1_msec ; count = 0; CLRF count ; while (count<80) m001 MOVLW .80 SUBWF count,W BTFSC 0x03,Carry GOTO m002 ; { ; count++; INCF count,1 ; } GOTO m001 ; } m002 RETLW .0 I count 7 instruction cycles through that loop (so again, 11 would not be good, but isn't unthinkable.) Lastly, it's a delay loop. What do you care how fast it does nothing? One uses a compiler largely because it does as well as you could do in assembly for COMPLEX operations, not trivial ones... BillW -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist