Efficiency depends on the compiler and how you write the code. Some compilers are notorious to have excellent optimizers (when told to do so), others not so. In your examples you didn't show how the variables were declared, but if you use 'long' variables then your code will be less efficient than if you use 'short' or even 'char'. As a rule of thumb, use the smallest type that can hold your maximum value. In your example you count up to 80, then such variable may be declared as a 'char' to get efficiency. The way you write things may affect the efficiency also. For instance: " char count; for( count =3D 0; count < 80; count++ ) ... " will probably be less efficient than: " char count; for( count =3D 80; count !=3D 0; count-- ) ... " Of course it all depends on the compiler, but the second form will probably run faster and take less code than the first. When using pointers and arrays, usually: " char i; char v[64]; for( i =3D 0; i < sizeof v; i++ ) putch( v[i] ); " will be less efficient than: " char i; char v[64] char *p; for( i =3D sizeof v, p =3D v; i !=3D 0; i--, p++ ) putch( *p ); " even more if 'v' is not 'char[]' and 'p' is not 'char*', because of a possible multiplication. Best regards, Isaac Em 22/2/2010 22:20, Jason Hsu escreveu: > Thanks for your responses. > > I tried writing a delay in C code, and the microcontroller doesn't > seem efficient in the counting process. The microcontroller can > increment a variable only 80 times in 1 msec. So that means that it > takes 12.5 usec to increment a variable just one time. With a 3.57945 > MHz crystal, a clock cycle is .279 usec. 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. > > I understand now that delays are usually executed in Assembly code. > What is the proper way to insert Assembly language code into a C > program for PICC? I recall trying to do this once but having > difficulty getting it to compile, so I gave up. > > My new code snippit is: > > void delay_1_msec (void) > { > count =3D 0; > while (count<80) > { > count++; > } > } > > void delay_10_msec (void) > { > count_10_msec =3D 0; > while (count_10_msec<10) > { > delay_1_msec(); // 10 delays of 1 msec each. > count_10_msec++; > } > } > > void delay_1_sec (void) > { > count_1_sec =3D 0; > while (count_1_sec<100) > { > delay_10_msec(); // 100 delays of 10 msec each. > count_1_sec++; > } > } > > = __________________________________________________ Fa=E7a liga=E7=F5es para outros computadores com o novo Yahoo! Messenger = http://br.beta.messenger.yahoo.com/ = -- = http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist