DSPRelated.com
Forums

C/C++ speed optimization bible/resources/pointers needed!

Started by Unknown July 26, 2007
C/C++ speed optimization bible/resources/pointers needed!

Hi all,

I am in the middle of programming to solve an engineering problem
where the speed is huge concern. The project involving lots of
numerical integration and then there are several loops/levels of
optimization on top of the function evaluation engine. As you probably
know, the key to a successful optimization is a fast underlying
objective function evaluator. The faster it is, the more promising the
optimization result(perhaps global optimal). However our project
requires many numerical integrations which prohibits us from making it
super fast. At the heart of the numerical integration is a smart
integrator and a super-fast integrand function evaluator. Even worse,
our function evaluation is in complex-domain. So the kay point is how
to arrange our C/C++ code to make it highly efficient in every aspect.
Could anybody give some advice/pointers on how to improve the speed of
C/C++ program? How to arrange code? How to make it highly efficient
and super fast? What options do I have if I don't have luxury to use
multi-threaded, multi-core or distributed computing? But I do have a
P4 at least. Please recommend some good bibles and resources! Thank
you!

In sci.physics lunamoonmoon@gmail.com wrote:
> C/C++ speed optimization bible/resources/pointers needed!
> Hi all,
> I am in the middle of programming to solve an engineering problem > where the speed is huge concern. The project involving lots of > numerical integration and then there are several loops/levels of > optimization on top of the function evaluation engine. As you probably > know, the key to a successful optimization is a fast underlying > objective function evaluator. The faster it is, the more promising the > optimization result(perhaps global optimal). However our project > requires many numerical integrations which prohibits us from making it > super fast. At the heart of the numerical integration is a smart > integrator and a super-fast integrand function evaluator. Even worse, > our function evaluation is in complex-domain. So the kay point is how > to arrange our C/C++ code to make it highly efficient in every aspect. > Could anybody give some advice/pointers on how to improve the speed of > C/C++ program? How to arrange code? How to make it highly efficient > and super fast? What options do I have if I don't have luxury to use > multi-threaded, multi-core or distributed computing? But I do have a > P4 at least. Please recommend some good bibles and resources! Thank > you!
If the problem is ameniable to being multi-threaded, you are dismissing what would probably be the easiest to implement and biggest speed improvement. Multi-processor Linux boxes aren't all that expensive, expecially when you start looking at labor cost to hand optimize. -- Jim Pennino Remove .spam.sux to reply.
On 26 juil, 17:19, lunamoonm...@gmail.com wrote:
> C/C++ speed optimization bible/resources/pointers needed! > > Hi all, > > I am in the middle of programming to solve an engineering problem > where the speed is huge concern. The project involving lots of > numerical integration and then there are several loops/levels of > optimization on top of the function evaluation engine. As you probably > know, the key to a successful optimization is a fast underlying > objective function evaluator. The faster it is, the more promising the > optimization result(perhaps global optimal). However our project > requires many numerical integrations which prohibits us from making it > super fast. At the heart of the numerical integration is a smart > integrator and a super-fast integrand function evaluator. Even worse, > our function evaluation is in complex-domain. So the kay point is how > to arrange our C/C++ code to make it highly efficient in every aspect. > Could anybody give some advice/pointers on how to improve the speed of > C/C++ program? How to arrange code? How to make it highly efficient > and super fast? What options do I have if I don't have luxury to use > multi-threaded, multi-core or distributed computing? But I do have a > P4 at least. Please recommend some good bibles and resources! Thank > you!
Your best bet with C/C++ is to get a fast computer. Optimization is only nominal with these compilers. If you have the option, the fastest language all around, due to structurally built in low level code optimization would be get a Forth language interpreter/compiler. Short of going to straight assembly language, nothing is faster. Andr=E9 Michaud
In article <1185484775.445904.137740@g4g2000hsf.googlegroups.com>, lunamoonmoon@gmail.com wrote:

!! C/C++ speed optimization bible/resources/pointers needed!
!! 
!! Hi all,
!! 
!! I am in the middle of programming to solve an engineering problem
!! where the speed is huge concern. The project involving lots of
!! numerical integration and then there are several loops/levels of
!! optimization on top of the function evaluation engine. As you probably
!! know, the key to a successful optimization is a fast underlying
!! objective function evaluator. The faster it is, the more promising the
!! optimization result(perhaps global optimal). However our project
!! requires many numerical integrations which prohibits us from making it
!! super fast. At the heart of the numerical integration is a smart
!! integrator and a super-fast integrand function evaluator. Even worse,
!! our function evaluation is in complex-domain. So the kay point is how
!! to arrange our C/C++ code to make it highly efficient in every aspect.
!! Could anybody give some advice/pointers on how to improve the speed of
!! C/C++ program? How to arrange code? How to make it highly efficient
!! and super fast? What options do I have if I don't have luxury to use
!! multi-threaded, multi-core or distributed computing? But I do have a
!! P4 at least. Please recommend some good bibles and resources! Thank
!! you!

your best friend is a good optimising compiler

in my experience
  codewarrior was top dog when metrowerks owned it
  but now it looks like intel has the best reputation

you have to run benchmarks and profile code execution paths
  but just choosing the right compiler has increased code speed 40% in tight loops for me in the past

( in fact - always verify with profiling what needs work
  otherwise you will almost certainly waste time "optimising" nonessential areas )

avoid unnecessary copies

that may seem obvious
  but that means all nonfundamental types should be passed as a const reference for inputs
  and you may need to use "expression templates" to prevent certain intermediate copies in looped evaluations

see "c++ templates" by vandevoorde and josuttis for a description of expression templates
  as applied to matrix computations and related problems

experiment with unrolling loops
  you don't want to unroll too much and have your code walking all over your cache
  but too little wastes unnecessary time messing with the iterator
there is usually a sweet spot

in general
  any calculation that can be done during translationtime
  saves you runtime

the compiler can usually do much of this
  but look at other techniques like metaprogramming if profiling shows you are still wasting a lot of time here

cf. abrahams and gurtovoy's "c++ template metaprogramming"
or czarnecki and eisenecker's "generative programming" for similar methods

use the bit width of your processor
  if you have (as you mention with p4) a 32-bit processor
  be wary of data structures that use 8-bit chars (if they are that on your architecture)

i found once that a cryptographic routine that looped over chars for encryption
  began running 10x faster (not just 4!) when i 32-bitised all the operations
because address space alignment is precious at the hardware level

also
  you may benefit from branch prediction hints
compilers like gcc have hints you can add to if statements (likely/unlikely)
that can optimise the most used branches

i have not found that as useful in my work but it is used extensively in the linux kernel

and ask the right groups for help
  fortran users are less likely to know c++ tricks than c++ users
  ( probably because they still think its faster despite the optimisations
    that expression templates allow over it! )

i hope this helps some

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 
galathaea: prankster, fablist, magician, liar 
In sci.physics lunamoonmoon@gmail.com wrote:
> C/C++ speed optimization bible/resources/pointers needed!
> Hi all,
> I am in the middle of programming to solve an engineering problem > where the speed is huge concern. The project involving lots of > numerical integration and then there are several loops/levels of > optimization on top of the function evaluation engine. As you probably > know, the key to a successful optimization is a fast underlying > objective function evaluator. The faster it is, the more promising the > optimization result(perhaps global optimal). However our project > requires many numerical integrations which prohibits us from making it > super fast. At the heart of the numerical integration is a smart > integrator and a super-fast integrand function evaluator. Even worse, > our function evaluation is in complex-domain. So the kay point is how > to arrange our C/C++ code to make it highly efficient in every aspect. > Could anybody give some advice/pointers on how to improve the speed of > C/C++ program? How to arrange code? How to make it highly efficient > and super fast? What options do I have if I don't have luxury to use > multi-threaded, multi-core or distributed computing? But I do have a > P4 at least. Please recommend some good bibles and resources! Thank > you!
One more thought, both the OS and the compiler can make a big difference. Some years ago I did some benchmarking on a customer's application on windows, Linux, SCO Unix, Solaris X86, and Unixware both with the vendors compiler and GNU all on the same hardware. I lost the numbers long ago, but windows was the absolute slowest. Linux, Solaris X86, and the Sun compiler are all free for the download. It might not hurt to run some benchmarks on your app. -- Jim Pennino Remove .spam.sux to reply.
jimp@specsol.spam.sux.com wrote:
> In sci.physics lunamoonmoon@gmail.com wrote: >> C/C++ speed optimization bible/resources/pointers needed! > >> Hi all, > >> I am in the middle of programming to solve an engineering problem >> where the speed is huge concern. The project involving lots of >> numerical integration and then there are several loops/levels of >> optimization on top of the function evaluation engine. As you probably >> know, the key to a successful optimization is a fast underlying >> objective function evaluator. The faster it is, the more promising the >> optimization result(perhaps global optimal). However our project >> requires many numerical integrations which prohibits us from making it >> super fast. At the heart of the numerical integration is a smart >> integrator and a super-fast integrand function evaluator. Even worse, >> our function evaluation is in complex-domain. So the kay point is how >> to arrange our C/C++ code to make it highly efficient in every aspect. >> Could anybody give some advice/pointers on how to improve the speed of >> C/C++ program? How to arrange code? How to make it highly efficient >> and super fast? What options do I have if I don't have luxury to use >> multi-threaded, multi-core or distributed computing? But I do have a >> P4 at least. Please recommend some good bibles and resources! Thank >> you! > > If the problem is ameniable to being multi-threaded, you are dismissing > what would probably be the easiest to implement and biggest speed > improvement. > > Multi-processor Linux boxes aren't all that expensive, expecially > when you start looking at labor cost to hand optimize.
It doesn't matter how many threads there are. What matters is the number of processor cycles that can be dedicated to the task in a given time. Jerry -- Engineering is the art of making what you want from things you can get. &macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;
srp@microtec.net wrote:
> On 26 juil, 17:19, lunamoonm...@gmail.com wrote: >> C/C++ speed optimization bible/resources/pointers needed! >> >> Hi all, >> >> I am in the middle of programming to solve an engineering problem >> where the speed is huge concern. The project involving lots of >> numerical integration and then there are several loops/levels of >> optimization on top of the function evaluation engine. As you probably >> know, the key to a successful optimization is a fast underlying >> objective function evaluator. The faster it is, the more promising the >> optimization result(perhaps global optimal). However our project >> requires many numerical integrations which prohibits us from making it >> super fast. At the heart of the numerical integration is a smart >> integrator and a super-fast integrand function evaluator. Even worse, >> our function evaluation is in complex-domain. So the kay point is how >> to arrange our C/C++ code to make it highly efficient in every aspect. >> Could anybody give some advice/pointers on how to improve the speed of >> C/C++ program? How to arrange code? How to make it highly efficient >> and super fast? What options do I have if I don't have luxury to use >> multi-threaded, multi-core or distributed computing? But I do have a >> P4 at least. Please recommend some good bibles and resources! Thank >> you! > > Your best bet with C/C++ is to get a fast computer. Optimization > is only nominal with these compilers. > > If you have the option, the fastest language all around, due to > structurally built in low level code optimization would be get > a Forth language interpreter/compiler. > > Short of going to straight assembly language, nothing is faster.
What's more, inlining assembly code is an inherent part of the language. Optimizing native-code Forth compilers are not available for many embeddable processors, but there are some. Check http://www.forth.com/ and http://www.mpeltd.demon.co.uk/ for starters. Jerry -- Engineering is the art of making what you want from things you can get. &macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;
On 26 juil, 20:46, Jerry Avins <j...@ieee.org> wrote:
> s...@microtec.net wrote: > > On 26 juil, 17:19, lunamoonm...@gmail.com wrote: > >> C/C++ speed optimization bible/resources/pointers needed! > > >> Hi all, > > >> I am in the middle of programming to solve an engineering problem > >> where the speed is huge concern. The project involving lots of > >> numerical integration and then there are several loops/levels of > >> optimization on top of the function evaluation engine. As you probably > >> know, the key to a successful optimization is a fast underlying > >> objective function evaluator. The faster it is, the more promising the > >> optimization result(perhaps global optimal). However our project > >> requires many numerical integrations which prohibits us from making it > >> super fast. At the heart of the numerical integration is a smart > >> integrator and a super-fast integrand function evaluator. Even worse, > >> our function evaluation is in complex-domain. So the kay point is how > >> to arrange our C/C++ code to make it highly efficient in every aspect. > >> Could anybody give some advice/pointers on how to improve the speed of > >> C/C++ program? How to arrange code? How to make it highly efficient > >> and super fast? What options do I have if I don't have luxury to use > >> multi-threaded, multi-core or distributed computing? But I do have a > >> P4 at least. Please recommend some good bibles and resources! Thank > >> you! > > > Your best bet with C/C++ is to get a fast computer. Optimization > > is only nominal with these compilers. > > > If you have the option, the fastest language all around, due to > > structurally built in low level code optimization would be get > > a Forth language interpreter/compiler. > > > Short of going to straight assembly language, nothing is faster. > > What's more, inlining assembly code is an inherent part of the language. > Optimizing native-code Forth compilers are not available for many > embeddable processors, but there are some. Checkhttp://www.forth.com/ > andhttp://www.mpeltd.demon.co.uk/for starters. > > Jerry > -- > Engineering is the art of making what you want from things you can get.
That's optimization!!! ;-)
On Jul 26, 5:19 pm, lunamoonm...@gmail.com wrote:
> C/C++ speed optimization bible/resources/pointers needed! > > Hi all, > > I am in the middle of programming to solve an engineering problem > where the speed is huge concern. The project involving lots of > numerical integration and then there are several loops/levels of > optimization on top of the function evaluation engine. As you probably > know, the key to a successful optimization is a fast underlying > objective function evaluator. The faster it is, the more promising the > optimization result(perhaps global optimal). However our project > requires many numerical integrations which prohibits us from making it > super fast. At the heart of the numerical integration is a smart > integrator and a super-fast integrand function evaluator. Even worse, > our function evaluation is in complex-domain. So the kay point is how > to arrange our C/C++ code to make it highly efficient in every aspect. > Could anybody give some advice/pointers on how to improve the speed of > C/C++ program? How to arrange code? How to make it highly efficient > and super fast? What options do I have if I don't have luxury to use > multi-threaded, multi-core or distributed computing? But I do have a > P4 at least. Please recommend some good bibles and resources! Thank > you!
You haven't described the problem in sufficient detail to make specific recommendations. You should look for ways to to reuse function evaluations. At one level the numerical integrator should be (probably is) doing this, and it isn't clear what you mean by saying "our function evaluation is in complex-domain". You may have some opportunities if you are carrying out line integrations in the complex plane to pick up several of the function values you need in one pass of the line integration, or to pick up a line integration from a nearby point at which a previous pass left off. regards, chip
In sci.physics Jerry Avins <jya@ieee.org> wrote:
> jimp@specsol.spam.sux.com wrote: > > In sci.physics lunamoonmoon@gmail.com wrote: > >> C/C++ speed optimization bible/resources/pointers needed! > > > >> Hi all, > > > >> I am in the middle of programming to solve an engineering problem > >> where the speed is huge concern. The project involving lots of > >> numerical integration and then there are several loops/levels of > >> optimization on top of the function evaluation engine. As you probably > >> know, the key to a successful optimization is a fast underlying > >> objective function evaluator. The faster it is, the more promising the > >> optimization result(perhaps global optimal). However our project > >> requires many numerical integrations which prohibits us from making it > >> super fast. At the heart of the numerical integration is a smart > >> integrator and a super-fast integrand function evaluator. Even worse, > >> our function evaluation is in complex-domain. So the kay point is how > >> to arrange our C/C++ code to make it highly efficient in every aspect. > >> Could anybody give some advice/pointers on how to improve the speed of > >> C/C++ program? How to arrange code? How to make it highly efficient > >> and super fast? What options do I have if I don't have luxury to use > >> multi-threaded, multi-core or distributed computing? But I do have a > >> P4 at least. Please recommend some good bibles and resources! Thank > >> you! > > > > If the problem is ameniable to being multi-threaded, you are dismissing > > what would probably be the easiest to implement and biggest speed > > improvement. > > > > Multi-processor Linux boxes aren't all that expensive, expecially > > when you start looking at labor cost to hand optimize.
> It doesn't matter how many threads there are. What matters is the number > of processor cycles that can be dedicated to the task in a given time.
And for a multi-processor box you get processor cycles times the number of CPUs assuming your OS is a true multi-processor OS. A threaded application will run slower on a single CPU machine than the same application none-threaded. If one assumes the machine is dedicated to running the application, the close to maximum performance will be when there is the same number of CPUs as there are threads. You may get a slight improvement if there are threads + 1 CPUs. If you are doing other things, then you need a CPU per thread plus enough CPUs to handle whatever else is going on to get max performance. And, you need to have enough memory to keep everything in memory or things slow down with disk swapping. -- Jim Pennino Remove .spam.sux to reply.