Code optimization

Topics related to the API, programming discussions & questions, coding tips, bugs, etc. should go here.
User avatar
DaveyPocket
Posts: 378
Joined: Sun Sep 14, 2008 8:33 pm
Contact:

Code optimization

Post by DaveyPocket »

Maybe we should start a thread in the wiki for optimizing code. For example; instead of doing something like this:

Code: Select all

if (x > 10){
x = 0
};
x++
One could do this:

Code: Select all

x = (x+1)%11;
Doing this saves a few bytes, and it does work. So if anyone has any tricks for saving memory space and speed, I think we should share our ideas (that is, if you want).
User avatar
uze6666
Site Admin
Posts: 4801
Joined: Tue Aug 12, 2008 9:13 pm
Location: Montreal, Canada
Contact:

Re: Code optimization

Post by uze6666 »

Sure why not, we already have one for assembler tricks & tips. I've created a new page:http://uzebox.org/wiki/index.php?title= ... imizations . To all, feel free to contribute content.

-Uze
User avatar
paul
Posts: 457
Joined: Sat May 02, 2009 8:41 am
Location: Brisbane, Australia

Re: Code optimization

Post by paul »

Good idea, Davey. I'll try to make a note of some typical ones I take for granted and add to the list.

Strangely enough, I think your example may end up costing you more than it helps and is not necessarily equivalent without context. I currently use the % operator for SLOW_BG_PATTERNS in Platz and you can see just how badly it affects performance. I'm going to change this to the if,then format in a future version because it increases performance massively. Sometimes % is needed, but usually when counting up to a limit, the other is better. I imagine X%(2^n) is an exception because you can perform it as X&(2^n-1). Also, you probably have to include library code for that % operator if you have not used it elsewhere in similar circumstances. It's one of the main reasons I want to learn asm - a lot of the time I use relatively terse C syntax without realizing the compiler may have turned it into unwieldy asm. I'm also keen to investigate the emulator scene, and a good grasp of asm over a range of platforms appears to be a prerequisite.
scuzz
Posts: 73
Joined: Sun Sep 13, 2009 4:57 am
Location: Maryland, USA
Contact:

Re: Code optimization

Post by scuzz »

As paul says, mod is a very expensive operation (unless you're doing mod with a power of 2!). What it saves you in memory is more then made up for in loss of speed!

That being said, does it really save you a few bytes? It may save a few bytes in the C source code, but when it compiles, the expansion of that routine seems like it would likely be pretty big (maybe this is compiled in even if you don't use it?)... Also seems like if it is a few bytes smaller that it may take up more ram during execution where it stores temporary information trying to solve the problem (or at least some some looping construct)...
User avatar
DaveyPocket
Posts: 378
Joined: Sun Sep 14, 2008 8:33 pm
Contact:

Re: Code optimization

Post by DaveyPocket »

I don't exactly remember, I'll have to play around with this stuff to get more results.
smith2287
Posts: 4
Joined: Mon Oct 12, 2009 7:28 am

Re: Code optimization

Post by smith2287 »

DaveyPocket wrote:Maybe we should start a thread in the wiki for optimizing code. For example; instead of doing something like this:

Code: Select all

if (x > 10){
x = 0
};
x++
One could do this:

Code: Select all

x = (x+1)%11;
Doing this saves a few bytes, and it does work. So if anyone has any tricks for saving memory space and speed, I think we should share our ideas (that is, if you want).
Good idea!
Hey Deavey
can you give me solution for swapping two variables without using 3rd temporary variable.
Last edited by smith2287 on Thu Oct 22, 2009 1:52 pm, edited 1 time in total.
User avatar
JRoatch
Posts: 108
Joined: Mon May 11, 2009 11:48 pm
Contact:

Re: Code optimization

Post by JRoatch »

I know the XOR swap algorithm by heart.
a ^= b;
b ^= a;
a ^= b;

for swapping registers in assembly.
EOR r8, r9
EOR r9, r8
EOR r8, r9
User avatar
uze6666
Site Admin
Posts: 4801
Joined: Tue Aug 12, 2008 9:13 pm
Location: Montreal, Canada
Contact:

Re: Code optimization

Post by uze6666 »

You can also use 1 of the 3 General Purpose I/O Register:

Code: Select all

out r8,GPIOR1
mov r8,r9
in r9,GPIOR1
-Uze
orthopteroid
Posts: 12
Joined: Mon Jan 18, 2010 8:08 am
Location: Vancouver, Canada

Re: Code optimization

Post by orthopteroid »

Hi all. A couple of questions...

I've a non-debug build of a simple test app and was wondering if anyone might know why I find malloc and free lurking in my lss file... I don't call them and can't find any kernel sources that use them. How are these clib routines getting linked in there?

avr-size reports that my build uses 3352 bytes of 'data' and 10746 bytes of 'program' - this is confusing. Where should I look to determine how much space is left on the chip?

Has anyone worked on a handle-based un/lock memory system for this little guy?

cheers
User avatar
D3thAdd3r
Posts: 3222
Joined: Wed Apr 29, 2009 10:00 am
Location: Minneapolis, United States

Re: Code optimization

Post by D3thAdd3r »

avr-size reports that my build uses 3352 bytes of 'data' and 10746 bytes of 'program' - this is confusing. Where should I look to determine how much space is left on the chip?
For the '644 it would just be 4k(4096-3352)=744 data free and 64k(65535-10746)=54789 program free, if you don't mind manually figuring it out. Otherwise I'm not sure.
Post Reply