How to speed optimize c/c++ code 
Author Message
 How to speed optimize c/c++ code

Quote:
> I need some hints on how to speed optimize C/C++ code. Is there a
> list about what function uses how many operations?

I would suggest profiling your code to see what function or part of your
program takes the longest to execute, and then to try to optimize that
function.

John



Fri, 28 Jan 2000 03:00:00 GMT  
 How to speed optimize c/c++ code

Hello C/C++ cracks

I need some hints on how to speed optimize C/C++ code. Is there a
list about what function uses how many operations?


Thanks in advance
Stefan Schenkel

\****           Name  : Stefan Schenkel                         ****/

  \**           S-Mail: Jurastrasse 5, CH - 2502 Biel           **/
   \*           Voice : 011 41 32 323 78 27                     */



Fri, 28 Jan 2000 03:00:00 GMT  
 How to speed optimize c/c++ code

Quote:

> I need some hints on how to speed optimize C/C++ code. Is there a
> list about what function uses how many operations?

There is absolutely no way to create such a list. It depends completely on
the compiler you are using and obviously on the machine for which the
compiler produces code.

And if you are interested in speed, then "how many operations" doesnt
help, because some operations take more time, some take less time, and it
depends on the order of these instructions and thousand other things. If
you are interested in these things, you might check
"http://www.mot.com/SPS/PowerPC/library/library.html" where you can
download everything about PowerPC processors. And I am sure there are
similar WEB pages for Intel, Alpha, MIPS, Sparc etc. Tough reading though.

What I recommend is: Get a compiler that comes with a profiler. Then
__measure__ how long things take. Without measuring you never know how
long an operation takes. __After__ measuring you know where your program
spends time and what might need improving.

As long as you are learning, it would be a good idea to take the results
from the profiler and compare it with what you expected.

-- For email responses, please remove the last emm from my address.



Fri, 28 Jan 2000 03:00:00 GMT  
 How to speed optimize c/c++ code


Quote:
>Hello C/C++ cracks

>I need some hints on how to speed optimize C/C++ code. Is there a
>list about what function uses how many operations?



Use fast algorithms and structures.
Avoid passing classes by value.

--
Ray Fischer        



Sun, 30 Jan 2000 03:00:00 GMT  
 How to speed optimize c/c++ code


to speed optimize c/c++ code, try inline assembly into your C code.
u can also look for virii mags as they too teach u to optimise codes...

Yours
Eugene Teo H. H.


Homepage: http://www.angelfire.com/me/eugeneteo * Demo *
          http://www.post1.com/~eugeneteo * Soooon *

* Keep trying. It's only the valley that the mountains seem high. *

Quote:


> > I need some hints on how to speed optimize C/C++ code. Is there a
> > list about what function uses how many operations?

> There is absolutely no way to create such a list. It depends completely on
> the compiler you are using and obviously on the machine for which the
> compiler produces code.

> And if you are interested in speed, then "how many operations" doesnt
> help, because some operations take more time, some take less time, and it
> depends on the order of these instructions and thousand other things. If
> you are interested in these things, you might check
> "http://www.mot.com/SPS/PowerPC/library/library.html" where you can
> download everything about PowerPC processors. And I am sure there are
> similar WEB pages for Intel, Alpha, MIPS, Sparc etc. Tough reading though.

> What I recommend is: Get a compiler that comes with a profiler. Then
> __measure__ how long things take. Without measuring you never know how
> long an operation takes. __After__ measuring you know where your program
> spends time and what might need improving.

> As long as you are learning, it would be a good idea to take the results
> from the profiler and compare it with what you expected.

> -- For email responses, please remove the last emm from my address.




Fri, 04 Feb 2000 03:00:00 GMT  
 How to speed optimize c/c++ code



Quote:
> In-Reply-To:


Quote:

> to speed optimize c/c++ code, try inline assembly into your C code.
> u can also look for virii mags as they too teach u to optimise codes...

Actually, using inline assembler is about the last thing I would recommend
to anybody. (Of course there are people who use assembler. And sometimes
there is a good reason to do so. But people who _ask_ how to optimise
shouldnt use assembler for serious work).

Sometimes code is slow only because the programmer did do something
extremly stupid without realising it, so some code ends up being much
slower than it should be and much slower than the programmer expects it to
be. You cant fix that by using assembler. You use a profiler.

If your sorting routine is called thousand times instead of once, you dont
fix it by rewriting it in assembler. You fix it by calling it only once.

-- For email responses, please remove the last emm from my address.



Sat, 05 Feb 2000 03:00:00 GMT  
 How to speed optimize c/c++ code


Quote:



>> In-Reply-To:

>> to speed optimize c/c++ code, try inline assembly into your C code.
>> u can also look for virii mags as they too teach u to optimise codes...

>Actually, using inline assembler is about the last thing I would recommend
>to anybody. (Of course there are people who use assembler. And sometimes
>there is a good reason to do so. But people who _ask_ how to optimise
>shouldnt use assembler for serious work).

>Sometimes code is slow only because the programmer did do something
>extremly stupid without realising it, so some code ends up being much
>slower than it should be and much slower than the programmer expects it to
>be. You cant fix that by using assembler. You use a profiler.

>If your sorting routine is called thousand times instead of once, you dont
>fix it by rewriting it in assembler. You fix it by calling it only once.

        As an interesting note. The use of the "inline" keyword on a
C++ function will cause it's complete code to be placed where the call
occurs. This saves the function call overhead.

        One time I thought this was a cure-all for speed but after
inlining about 20 functions, my code got slower.

        I really don't know exactly why, but I can only guess that
using inline increases your code size (depending on how often you call
the inlined function), and in that case it was just enough to cause
the processor cache to NOT be able to contain as much of my program,
thus, slower program.

        This is the argument you can use if people claim that function
call overhead always hurts performance.

        WH



Sat, 05 Feb 2000 03:00:00 GMT  
 How to speed optimize c/c++ code



Quote:
>Hello C/C++ cracks

>I need some hints on how to speed optimize C/C++ code. Is there a
>list about what function uses how many operations?

Not in general.  It varies too much from implementation to
implementation.  

There are two fundamental concepts for optimization:  algorithms
and profiling.

If your program runs slow enough that it's worth optimizing, it's
going to be seriously affected by what algorithms you're using.
Make sure you're using good ones.  If you're using a bubble sort,
rip it out.  If you're doing numeric integration by the trapezoidal
rule, don't.  Refining the algorithms is very useful, when applicable,
because it will speed the program up on any system.

Once you've got the program written, and you find it's too slow,
profile it.  Find out where it's spending most of its time.  Programs
normally have "hot spots" - small sections of code that take large
chunks of the execution time.  This will tell you where to work, and
may give you clues about what is taking so long, and how to make it
not take so long.

Unfortunately, most advice for speeding up these hot spots is
system-specific in nature.  You will probably need to figure out
what takes less time on your implementation, likely by experiment.
Write a routine that does something, put it in a loop so it's executed
a thousand or million or whatever times, measure how long that takes,
change the routine, and try it again.  If you're lucky, somebody
has already done things like this for your implementation, and
written something about optimizing.

David Thornley



Sat, 05 Feb 2000 03:00:00 GMT  
 How to speed optimize c/c++ code


Quote:

>If your sorting routine is called thousand times instead of once, you dont
>fix it by rewriting it in assembler. You fix it by calling it only once.

no, you use a binary insertion tree instead. this of course assumes
that the data structure is a correct one.


Sun, 06 Feb 2000 03:00:00 GMT  
 How to speed optimize c/c++ code


Quote:


>>Hello C/C++ cracks

>>I need some hints on how to speed optimize C/C++ code. Is there a
>>list about what function uses how many operations?

>Not in general.  It varies too much from implementation to
>implementation.  

>There are two fundamental concepts for optimization:  algorithms
>and profiling.

either that, or buy a scalable computer...


Sun, 06 Feb 2000 03:00:00 GMT  
 How to speed optimize c/c++ code


Quote:


>>Hi there,


>>>Hello C/C++ cracks

>>>I need some hints on how to speed optimize C/C++ code. Is there a
>>>list about what function uses how many operations?

The best thing you can do is run your code through a profiler.
Profilers are included with most commercial compilers, I know
that Borland, Watcom and Microsoft include them.
If you're using unix, look at the man pages for prof and gprof.

The reason for this, is that it's not always clear to the programmer
where all the time is going. For example, I wrote a program that
as well as other things, logged on to a database, retrieved about
a hundred records, stored them in an stl vector, sorted the
vector, and did some other processing. It turned out that the
database log on (not the retrieve) took much longer than the
vector sort. I had worried about the sort time, but that was
so fast that there was no reason to try to optimize it.
But from the programmer's point of view, that would be
the loop that executed the largest number of times.

So it's not the number of operations in each function, it's the
real time that counts. Believe me, if you start optimizing
without using a profiler, you'll be wasting lots of time.

Regards,

--
// Michael Davis, Programmer/Analyst      I don't speak for Multipath.  //

// Toronto             The 'DEL' in my address is an anti-spam device.  //



Sun, 06 Feb 2000 03:00:00 GMT  
 How to speed optimize c/c++ code



Quote:
>Hi there,


>>Hello C/C++ cracks

>>I need some hints on how to speed optimize C/C++ code. Is there a
>>list about what function uses how many operations?

>There are some simple things you have to do if you are writing a program
>in ANY language:

>1) optimize loops
>Only put code in a loop that has to do something with it. Initialisation
>stuff has to be put outside.

This is generally true although compilers can deal with loop invariants quite
effectively.

Quote:
>Use registers in loops.

Compilers these days can generally allocate register gar more effectively
than using clumsy directives in source code. In some cases they can make
matters worse.

Quote:
>Optimize the head! The less comparisons you'll use, the faster your code
>will be.

More generally the fewer operations you perform the faster your code will
be. Unfortunately this doesn't take into account the relative costs
of different operations and how well these can be parallelised on superscalar
archetectures. In particular comparisons can be very low cost operations
on archetectures with good branch prediction.

Quote:
>2) do pointer arithmetic
>Use pointers at every time, it's possible. Never use p[x] or these

This is simply wrong. Firstly [] is a pointer operator (it requires a pointer
and an integer offset as opeands). When people say they what they are
generally comparing are:

1. Walking through an array by incrementing and decrementing a pointer

2. Walking through an array using an invariant pointer and integer offset.

The 2nd case generally more scope for an optimiser so when the compiler
doesn't simple produce the same code for both cases there is a good chance
that the 2nd case will me more efficient.

Quote:
>3) do logical stuff on integers
>Think of speeding up your comparisons. Use !a instead of (a==0), !(a|b)
>instead of (a==0 && b==0), and so on.

Any compiler that can't do this for itself (even with optimisation disabled)
is going to have far more serious optimisation deficiencies. The only thing
to do in that situation is get a better compiler.

Quote:
>Think of De Morgan's rules.

It is precisely because there is a simple set of rules for this that the
camiler can optimise this trivially.

Quote:
>4) use compiler options
>Every compiler does optimization stuff. Be careful with compilers that
>let you only opimize speed or size. Thats junk.

???

Quote:
>Look at gcc.

gcc has a competent optimiser. There are many compilers out there that
can do a better job. The advantage of gcc is the range of platforms
it supports.

Quote:
>5) think of your hardware
>If you'll do heavy optimization, you'll have to look at your processors,
>bus, memory specs. Think of things like prefetch-buffer, L1/L2-cache,
>latency, etc.
>You don't need that stuff, if you'll only run fast. You'll need it, if
>you'll run faster ;-).

If speed is that critical write in assembly. It is rarely worth doing that
degree of analysis for C code.

Quote:
>6) ... there are even more, but that's going too far.

>For sure, you've to look at the algorithms and data-structures you're
>using.

Which of course if *far* more important than anything else you've
mentioned here.

--
-----------------------------------------


-----------------------------------------



Sun, 06 Feb 2000 03:00:00 GMT  
 How to speed optimize c/c++ code


says...

Quote:


> >         As an interesting note. The use of the "inline" keyword on a
> > C++ function will cause it's complete code to be placed where the call
> > occurs. This saves the function call overhead.

> >         One time I thought this was a cure-all for speed but after
> > inlining about 20 functions, my code got slower.

> >         I really don't know exactly why, but I can only guess that
> > using inline increases your code size (depending on how often you call
> > the inlined function), and in that case it was just enough to cause
> > the processor cache to NOT be able to contain as much of my program,
> > thus, slower program.

Many possible causes of this slow-down including: Loop became too big
to fit in cache memory, optimization pass failed to do as good a job
on more complex function, extra page faults caused by larger virtual
memory size, ...

Inline code works best with very small/simple funcitons that the
optimizer can play many games with. Any kind of loop/conditional
in inline code seems to defeat the inlining on many compilers.

Quote:

>Worse still, a compiler can decide not to take the hint to inline
>and put a copy of the function in each object file that references
>it. You lose the benefits of inlining (i.e. no call overhead) and
>you have multiple copies of the one function in th one executale.

>Paul



Sun, 06 Feb 2000 03:00:00 GMT  
 How to speed optimize c/c++ code



: >I need some hints on how to speed optimize C/C++ code. Is there a
: >list about what function uses how many operations?

: Not in general.  It varies too much from implementation to
: implementation.

But time and space complexities, which are implementation-independent
properties of algorithms, are available.  Stefan will be best served
by looking for a book introducing algorithms.

: There are two fundamental concepts for optimization:  algorithms
: and profiling.

I don't see these two as being different concepts.  I can very well
profile my program to decide which algorithm is worth replacing.

: If your program runs slow enough that it's worth optimizing, it's
: going to be seriously affected by what algorithms you're using.
: Make sure you're using good ones.  If you're using a bubble sort,
: rip it out.  If you're doing numeric integration by the trapezoidal
: rule, don't.

Why should I optimize by using a more efficient (and almost always
more complicated) algorithm that ultimately has no impact on the
overall performance?  Why should I replace a bubblesort of ten
elements that are executed right before a call to fgets()?

: Refining the algorithms is very useful, when applicable,
: because it will speed the program up on any system.

Not true.  Algorithms almost always have space-time tradeoffs
(the less space required, the slower), and the price of this
space is very system-dependent.  For example, the overall
performance of algorithm A using 4KB of space may be actually
better than algorithm B using 5KB of space, even though B is
theoretically faster (better time complexity).  This might be
because the computer has a 4KB cache!

However, the statement will be true if you change it to
"almost any system".

: Once you've got the program written, and you find it's too slow,
: profile it.  Find out where it's spending most of its time.  Programs
: normally have "hot spots" - small sections of code that take large
: chunks of the execution time.  This will tell you where to work, and
: may give you clues about what is taking so long, and how to make it
: not take so long.

Very true.

: Unfortunately, most advice for speeding up these hot spots is
: system-specific in nature.

I disagree.  I would encourage the choice of simple and not terribly
slow solutions in the first attempt at a program.  Pick reasonable
algorithms (this requires that you have an arsenal of algorithms,
naturally).  When you have a working program that is too slow,
profile it and seek algorithmic improvements (which are not always
portably more efficient but will at least work).  If even that fails,
seek system-specific improvements.

If portability is not a prime concern, system-specific improvements
can of course be made together with algorithmic improvements (i.e.
write a better algorithm using assembly).

: You will probably need to figure out
: what takes less time on your implementation, likely by experiment.

Or reading a book.  How do you propose to empirically determine the
optimal instruction sequence for superscalar out-of-order machines,
for example?

: Write a routine that does something, put it in a loop so it's executed
: a thousand or million or whatever times, measure how long that takes,
: change the routine, and try it again.

This is, IMHO, the worst way to develop software.  Even if your
test routine finds you the fastest sequence, you have little
assurance that the performance tested can be sustained in an
actual program.  Instruction scheduling plays an increasingly
large role in performance, for instance, and that is not something
you can hope to be good at through blind trial-and-error!

: If you're lucky, somebody
: has already done things like this for your implementation, and
: written something about optimizing.

Amen.



Sun, 06 Feb 2000 03:00:00 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Bad Address from Speed Optimized Code

2. How to optimize execution speed of code ??

3. Problems with for speed optimized code

4. Problems with for speed optimized code

5. how to optimize this code for speed

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

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

8. Throwing exceptions in ATL dll with optimize for SPEED causes Access Violation

9. Speed/safety optimizing recomendation.

10. Problem with Optimize for speed in VC++ 5.0

11. Optimizing C compiler(speed)

12. Optimizing speed using inline assembly fails - Why?

 

 
Powered by phpBB® Forum Software