Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Thread

GCC - locals performance

GCC - locals performance

2005-12-14 by xjag74

Hi,

I wonder which kind of implementation well be performed faster using 
GCC:

void functionname(void)
{
  unsigned char x=0;
  unsigned char y=1;
  unsigned char z=2;

  ...Do something with x,y and z.
}

or

void functionname(void)
{
  unsigned long x=0;
  unsigned long y=1;
  unsigned long z=2;

  ...Do something with x,y and z.
}


The first example uses less stack, but it takes a longer time for 
initialization when calling the function cause the 8 bit have to be 
masked out.
Am I right with that supposition?

regards
xjag

Re: GCC - locals performance

2005-12-14 by brendanmurphy37

Hi,

The short answer is that it is impossible to say what either version 
does without looking at the assembler output from the compiler at 
the particular optimisation level you are using.

For example, at anything other than no optimisation, there's a fair 
chance no stack space will be used in either version, as the 
variables can be kept in registers.

At high optimisation levels, for anything other than very trivial 
functions, you'll have a tough time following what's going on. GCC 
does a pretty good job of optimisation, but the cost is that it is 
difficult to relate back to the source.

As a (very) general rule, unless there's some specific (usually 
space saving) requirement, on most processors and most compilers, 
using "int" or "unsigned int" will give the most efficient results. 
However, there's so many exceptions to this (e.g. int size of 32-
bits reading/writing across a 16-bit bus) that it's almost not worth 
bothering with.

By far the most important point I'd make is: "why do you care?". 
Unless the function in question is in the innermost part of a loop 
executed very many times, or is part of a very high frequency 
interrupt handler, I'd be willing to guess it has no effect on your 
system's performance. 

Brendan

--- In lpc2000@yahoogroups.com, "xjag74" <detlef.weidner@w...> wrote:
>
> Hi,
> 
> I wonder which kind of implementation well be performed faster 
using 
> GCC:
> 
> void functionname(void)
> {
>   unsigned char x=0;
>   unsigned char y=1;
>   unsigned char z=2;
> 
>   ...Do something with x,y and z.
> }
> 
> or
> 
> void functionname(void)
> {
>   unsigned long x=0;
>   unsigned long y=1;
>   unsigned long z=2;
> 
>   ...Do something with x,y and z.
> }
> 
> 
> The first example uses less stack, but it takes a longer time for 
> initialization when calling the function cause the 8 bit have to 
be 
Show quoted textHide quoted text
> masked out.
> Am I right with that supposition?
> 
> regards
> xjag
>

Re: [lpc2000] GCC - locals performance

2005-12-16 by 42Bastian Schick

xjag74 <detlef.weidner@...> schrieb am Wed, 14 Dec 2005 14:51:16 
-0000:
> void functionname(void)
> {
>   unsigned char x=0;
>
> or
>
> void functionname(void)
> {
>   unsigned long x=0;
>
> The first example uses less stack, but it takes a longer time for
> initialization when calling the function cause the 8 bit have to be
> masked out.
> Am I right with that supposition?

Both version use the same amount of stack since the stack must be 4 byte 
aligened on a ARM7.
The unsigned char version will in many cases need more instruction with gcc
because it often does a 8->32 bit promoting (although a ldrb would 
suffice).

My hint: Look at the assmbly output (-S option for gcc) !


-- 
42Bastian Schick

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.