Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

C Instructions Optimization

Status
Not open for further replies.

yatoof

New Member
Hi, All is there any guide(link,book). which tells about complexity of instructions we use in microcontroller's programming in c language e.g switch() is better or if statement.
I know it mostly depends upon compiler we are using. but any general help.
to make program most efficient fast for execution, with best choice of instructions like
if(SW) is better than if(SW==1)
Thanks in advance
.Yatoof
 
It is different for each compiler. In general as long as you keep your code simple the compiler will do a good job of optimizing it. Often times simple c code optimizes better then code where the author tried to optimize it in c.

I would not expect a lot of differance between code generated by a switch vrs a string of ifs.

All embedded IDE's should allow you to look at instructions generated by C code. Set a breakpoint int the code you want to look at. Run to that breakpoint then open the machine code code (disassembly) window.

Have fun.
 
You can optimize for smaller code which runs slower or larger code that runs faster. In genereral optimization is not much of an issue. For a few cents more you can buy a faster chip with more memory instead of spending many hours trying to squeeze out bytes or speed

Perhaps learning to write non-blocking code is a more useful thing to do. The idea behind non blocking code is that you never wait in a loop. If you need time to pass you stop the code that needs to wait and continue with the rest of the program until the stopped code is ready to run.

I wrote a c tutorial on how to do multitasking if you are interested. **broken link removed**

3v0
 
Status
Not open for further replies.

Latest threads

Back
Top