Writing optimized C code 
Author Message
 Writing optimized C code

Hi All,

I am searching for a documentation or a book recommendation of writing an
optimized C code. Where can I find such resources?

One example of such issue is:

// non optimized code:
int index;
for (index=0; index < count; index++)
{
    my_array[index].x = my_func(...);
    my_array[index].y = my_other_func(...);

Quote:
}

// Optimized and faster code (although less readable):
p_last_item = &my_array[count]; // point to one after the last item
for (p_my_array = &my_array[0]; p_my_array != p_last_item; p_my_array++)
{
    p_my_array->x = my_func(...);
    p_my_array->y = my_other_func(...);

Quote:
}

Thanks,
Eldad Kuperman
Texas Instruments Ltd.
--



Fri, 15 Oct 2004 11:09:21 GMT  
 Writing optimized C code

Quote:

> // Optimized and faster code ...

Not necessarily.  Modern compilers generate the same instructions
for either source code.  I suggest not trying to micro-optimize at
the source-code level, but rather express the algorithm as simply
and clearly as you can.  Then let the compiler do its job.
--



Sat, 16 Oct 2004 00:49:11 GMT  
 Writing optimized C code
[...]

Quote:
> // Optimized and faster code (although less readable):
> p_last_item = &my_array[count]; // point to one after the last item
> for (p_my_array = &my_array[0]; p_my_array != p_last_item; p_my_array++)
> {
>     p_my_array->x = my_func(...);
>     p_my_array->y = my_other_func(...);
> }

You can essentially forget about any such optimizations if you're
using any half-decent compiler of these days.  Compilers are quite
capable of finding and exploiting such opportunities on their own.

There's a large probability that you, as the programmer, will use up
more time writing and reading such modified code in its lifetime than
will ever be saved by it at runtime --- assuming it'll have any effect
at all.
--

Even if all the snow were burnt, ashes would remain.
--



Sat, 16 Oct 2004 00:49:22 GMT  
 Writing optimized C code

Quote:

> Hi All,

> I am searching for a documentation or a book recommendation of writing an
> optimized C code. Where can I find such resources?

> One example of such issue is:

> // non optimized code:
> int index;
> for (index=0; index < count; index++)
> {
>     my_array[index].x = my_func(...);
>     my_array[index].y = my_other_func(...);
> }

> // Optimized and faster code (although less readable):
> p_last_item = &my_array[count]; // point to one after the last item
> for (p_my_array = &my_array[0]; p_my_array != p_last_item; p_my_array++)
> {
>     p_my_array->x = my_func(...);
>     p_my_array->y = my_other_func(...);
> }

I recommend Programming Pearls by Jon Bentley.

The change you made is often called "micro-optimization".  The effect of
such changes depends significantly on the particular target and
compiler.  What is an optimization for one compiler is a "pessimization"
for another!  Many compilers already perform these types of
optimizations automatically.  To get the most out of these techniques,
you need to study the particular compiler you are using -- looking at
generated code, timing benchmarks, etc.  This can be helpful for getting
the last bit of optimization out of a piece of code for a particular
target, but is probably wasted effort for portable code.

The bigger gains come from using higher-level optimizations, such as
selecting the appropriate algorithm, simplifying data structures,
selecting appropriate resolution, etc.

Thad
--



Sat, 16 Oct 2004 00:49:26 GMT  
 Writing optimized C code


Quote:
>I am searching for a documentation or a book recommendation of writing an
>optimized C code. Where can I find such resources?

>One example of such issue is:

>// non optimized code:
>int index;
>for (index=0; index < count; index++)
>{
>    my_array[index].x = my_func(...);
>    my_array[index].y = my_other_func(...);
>}

>// Optimized and faster code (although less readable):

You sure? Seriously there are so many factors that hand-optimised code
for speed not infrequently runs slower. And it is harder to get right,
maintain and frequently defeats sophisticated optimisers because your
intent is so obscure.

Quote:
>p_last_item = &my_array[count]; // point to one after the last item
>for (p_my_array = &my_array[0]; p_my_array != p_last_item; p_my_array++)
>{
>    p_my_array->x = my_func(...);
>    p_my_array->y = my_other_func(...);
>}

If you must try optimising there are a couple of books on the subject
whose reviews you will find at www.accu.org but please note that
optimisation techniques date very quickly as compilers get better.

--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Mon, 18 Oct 2004 05:28:37 GMT  
 Writing optimized C code


Quote:
>Hi All,

>I am searching for a documentation or a book recommendation of writing an
>optimized C code. Where can I find such resources?

>One example of such issue is:

>// non optimized code:
>int index;
>for (index=0; index < count; index++)
>{
>    my_array[index].x = my_func(...);
>    my_array[index].y = my_other_func(...);
>}

>// Optimized and faster code (although less readable):
>p_last_item = &my_array[count]; // point to one after the last item
>for (p_my_array = &my_array[0]; p_my_array != p_last_item; p_my_array++)
>{
>    p_my_array->x = my_func(...);
>    p_my_array->y = my_other_func(...);
>}

Compared to the function calls, your change will have minimal or
no impact if high compiler optimization is used (-O9 normally).

Starting with a smart approach to the problem, good algorithm(s),
and well structured (but not excessive) modularization, will
normally yield much higher gains.

Look inside the function calls for optimization opportunities, or
better still, profile your code to find out what's really using
up all the time, and eliminate that bottleneck first.

Try declaring the functions inline if your compiler supports it,
or get a compiler that does (e.g. gcc).

You could also try changing your struct into a single dimension
array, multiple separate arrays, use Duff's device to unroll the
loop, etc.

Sometimes the best optimization is using library functions
provided with your compiler and seeing if using them instead of
rolling your own code gives you a visible speedup. If the speedup
is not visible or at least measurable, it's not worth doing.

See "The Practice of Programming", Kernighan & Pike, ch.7,
Performance, for more ideas and examples.

--

Thanks. Take care, Brian Inglis         Calgary, Alberta, Canada


    fake address                use address above to reply


                                                spam traps
--



Thu, 21 Oct 2004 22:32:15 GMT  
 Writing optimized C code
My 2 cents. If my_array is global, the first code snippet is never
optimized by any compiler to look like the second. After all, my_func()
may change the value of my_array.

Quote:



>>Hi All,

>>I am searching for a documentation or a book recommendation of writing an
>>optimized C code. Where can I find such resources?

>>One example of such issue is:

>>// non optimized code:
>>int index;
>>for (index=0; index < count; index++)
>>{
>>   my_array[index].x = my_func(...);
>>   my_array[index].y = my_other_func(...);
>>}

>>// Optimized and faster code (although less readable):
>>p_last_item = &my_array[count]; // point to one after the last item
>>for (p_my_array = &my_array[0]; p_my_array != p_last_item; p_my_array++)
>>{
>>   p_my_array->x = my_func(...);
>>   p_my_array->y = my_other_func(...);
>>}

> Compared to the function calls, your change will have minimal or
> no impact if high compiler optimization is used (-O9 normally).

> Starting with a smart approach to the problem, good algorithm(s),
> and well structured (but not excessive) modularization, will
> normally yield much higher gains.

> Look inside the function calls for optimization opportunities, or
> better still, profile your code to find out what's really using
> up all the time, and eliminate that bottleneck first.

> Try declaring the functions inline if your compiler supports it,
> or get a compiler that does (e.g. gcc).

> You could also try changing your struct into a single dimension
> array, multiple separate arrays, use Duff's device to unroll the
> loop, etc.

> Sometimes the best optimization is using library functions
> provided with your compiler and seeing if using them instead of
> rolling your own code gives you a visible speedup. If the speedup
> is not visible or at least measurable, it's not worth doing.

> See "The Practice of Programming", Kernighan & Pike, ch.7,
> Performance, for more ideas and examples.

--



Sat, 13 Nov 2004 07:11:12 GMT  
 Writing optimized C code

Quote:

> My 2 cents. If my_array is global, the first code snippet is never
> optimized by any compiler to look like the second. After all, my_func()
> may change the value of my_array.

No, it may not.  Or, more precisely, only if my_array was named quite
badly, and is not actually an array.  An actual array cannot be
"changed" like that --- only a pointer into an array can.

--

Even if all the snow were burnt, ashes would remain.
--



Sun, 14 Nov 2004 08:25:43 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. writing optimized code

2. Optimizing random number generator (was Re: Optimizing code for tests)

3. Optimized code slower than debug code

4. Newbie: separate big .cs file into small .cs files

5. optimizing file writing

6. optimizing reads/writes to disk

7. Include code in other Cs files

8. newbe/cs student, need help w/ code

9. Compile CS source code using ICodeCompiler

10. cs source code

11. Optimizing programmer vs optimizing compiler (was: Re: Assembly vs c/c++)

12. Bad Address from Speed Optimized Code

 

 
Powered by phpBB® Forum Software