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

This is a discussion on C/C++ speed optimization bible/resources/pointers needed! within the Other Technologies forums in category; lunamoonmoon@gmail.com wrote: (snip) > 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! I usually do test to see where it is ...

Go Back   Application Development Forum > Other Technologies

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #21  
Old 07-27-2007, 02:00 AM
glen herrmannsfeldt
Guest
 
Default Re: C/C++ speed optimization bible/resources/pointers needed!

lunamoonmoon@gmail.com wrote:

(snip)

> 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!


I usually do test to see where it is spending the most time.
It might be that you already know that is in function evaluation.
If so, you either have to simplify the function or get by with
fewer evaluations.

Without tests, I would not be so sure, though.

I sometimes, on intel processors, use the time stamp counter
available with the RDTSC instruction. For IA32 that usually
requires a two instruction assembly program that returns a
64 bit integer in EDX:EAX. It is a little more complicated
for x86-64.

-- glen

Reply With Quote
  #22  
Old 07-27-2007, 02:31 AM
Hendrik van der Heijden
Guest
 
Default Re: C/C++ speed optimization bible/resources/pointers needed!

galathaea schrieb:

> 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


I've seen a 500% boost just from adding a compiler option (-mfpmath).
With this option, gcc uses SSE1/2 units for floating point calculations
(SISD, not vector code) instead of traditional x87 code.


Hendrik vdH
Reply With Quote
  #23  
Old 07-27-2007, 02:35 AM
Hendrik van der Heijden
Guest
 
Default Re: C/C++ speed optimization bible/resources/pointers needed!

glen herrmannsfeldt schrieb:
> lunamoonmoon@gmail.com wrote:
>
>> 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?


> I usually do test to see where it is spending the most time. [..]
> I sometimes, on intel processors, use the time stamp counter
> available with the RDTSC instruction.


I found the Intel VTune application to be very helpful. It can
run your program (with debug information), samples the CPU
performance counters and then gives you a list of all functions
and the CPU time (clocks) spend in each function. For each funtion,
you can see the CPU clocks spend per source line or per assembly
operation.


Hendrik vdH
Reply With Quote
  #24  
Old 07-27-2007, 02:55 AM
glen herrmannsfeldt
Guest
 
Default Re: C/C++ speed optimization bible/resources/pointers needed!

jimp@specsol.spam.sux.com wrote:

(snip)

> Sigh, Linux is free; all you have to do is download and install it.


> Linux will be a LOT faster than Windows when doing number crunching.


There is no reason Linux should be faster, in general, for number
crunching. The numeric instructions take the same number of cycles
independent of the OS. For speedy number crunching one should minimize
the amount of paging, which could be OS dependent, as could I/O.

If one is doing a lot of paging or I/O, then I would say that one
is not doing number crunching. (That is, the task is I/O limited
and not CPU limited.)

(Not that I don't agree that Linux is a much better system to
use than the others under consideration.)

-- glen

Reply With Quote
  #25  
Old 07-27-2007, 03:05 AM
Andreas Huennebeck
Guest
 
Default Re: C/C++ speed optimization bible/resources/pointers needed!

lunamoonmoon@gmail.com wrote:

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


Regardless of the task the first thing to do is to use a profiler
to find out where the CPU spends most of its time. Usually
it spends 80% of its time in 20% of the code.

When you know where the time is spent you can either optimize
the existing code manually (you got enough hints already) or use
a faster algorithm or program design.

Usually you trade memory and/or precision for speed, for example:
- if you have a function which consumes a lot of time you can
replace it with an approximation table (more memory, less precision)
- if a function often calculates the same result you can cache its
results for later reuse (more memory).

Bye
Andreas
--
Andreas Hünnebeck | email: acmh@gmx.de
----- privat ---- | www : http://www.huennebeck-online.de
Fax/Anrufbeantworter: 0721/151-284301
GPG-Key: http://www.huennebeck-online.de/public_keys/andreas.asc
PGP-Key: http://www.huennebeck-online.de/publ...gp_andreas.asc

Reply With Quote
  #26  
Old 07-27-2007, 03:24 AM
Phil Carmody
Guest
 
Default Re: C/C++ speed optimization bible/resources/pointers needed!

glen herrmannsfeldt <gah@ugcs.caltech.edu> writes:
> jimp@specsol.spam.sux.com wrote:
>
> (snip)
>
> > Sigh, Linux is free; all you have to do is download and install it.

>
> > Linux will be a LOT faster than Windows when doing number crunching.

>
> There is no reason Linux should be faster, in general, for number
> crunching.


When I compiled, using the same level of gcc, my number crunching code
(OversEis, for finding prime numbers) for Opteron, using nothing
but the i386 registers and the FPU, no MMX/SSE/whatever, the code
running on linux was 30% faster than the code running on windows.
(250s per test vs. 330s per test) Also, in windows, running 1 such
program was slower than running 2 in parallel (330s for 2 tests,
vs. 480s for 1 test - linux was 250s for 2 tests, 250s for 1 test).

Something ain't right in the state of 64-bit windows.

Phil
--
"Home taping is killing big business profits. We left this side blank
so you can help." -- Dead Kennedys, written upon the B-side of tapes of
/In God We Trust, Inc./.
Reply With Quote
  #27  
Old 07-27-2007, 04:01 AM
glen herrmannsfeldt
Guest
 
Default Re: C/C++ speed optimization bible/resources/pointers needed!

Hendrik van der Heijden wrote:

(snip about program timing)

> I found the Intel VTune application to be very helpful. It can
> run your program (with debug information), samples the CPU
> performance counters and then gives you a list of all functions
> and the CPU time (clocks) spend in each function. For each funtion,
> you can see the CPU clocks spend per source line or per assembly
> operation.


I would wonder a little if RDTSC for each statement wouldn't
be enough to actually effect the execution time. That is, it
changes the possible pipelining such that the timing may be somewhat
different. Still, it does sound like a good place to start.

-- glen

Reply With Quote
  #28  
Old 07-27-2007, 04:43 AM
Vladimir Vassilevsky
Guest
 
Default Re: C/C++ speed optimization bible/resources/pointers needed!


<lunamoonmoon@gmail.com> wrote in message
news:1185484775.445904.137740@g4g2000hsf.googlegro ups.com...
> C/C++ speed optimization bible/resources/pointers needed!
>


> I am in the middle of programming to solve an engineering problem
> where the speed is huge concern.


And the problem is?

> 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.


No. The key to successful optimization is good algorithm of optimization.

> The faster it is, the more promising the
> optimization result(perhaps global optimal).


Bruteforcing?

> However our project
> requires many numerical integrations which prohibits us from making it
> super fast.


Good. Integrals are well behaved functions.

> At the heart of the numerical integration is a smart
> integrator and a super-fast integrand function evaluator.


This is a first time I hear about the heart of integrator.

> Even worse,
> our function evaluation is in complex-domain.


It doesn't make much difference.

> So the kay point is how
> to arrange our C/C++ code to make it highly efficient in every aspect.


The key point is optimization of the algorithm, not the optimization of the
code.

> Could anybody give some advice/pointers on how to improve the speed of
> C/C++ program?


Overclock the CPU.

> How to arrange code?


In lines with indentation. Just don't use tabs. Use spaces.

> How to make it highly efficient
> and super fast?


If you work on the code for long and hard, it would be super efficient and
highly fast.

> What options do I have if I don't have luxury to use
> multi-threaded, multi-core or distributed computing?


Hire the consultant. At least you can stay assured that you took all
possible measures.

> But I do have a
> P4 at least.


You know, I do have P4, too.

> Please recommend some good bibles and resources!


Try www.biblegateway.com . One of the most comprehensive Bible collections.


Vladimir Vassilevsky
DSP and Mixed Signal Consultant
www.abvolt.com













Reply With Quote
  #29  
Old 07-27-2007, 07:30 AM
Luna Moon
Guest
 
Default Re: C/C++ speed optimization bible/resources/pointers needed!


<shalayka@gmail.com> wrote in message
news:1185508917.064166.239070@j4g2000prf.googlegro ups.com...
> On Jul 26, 9:59 pm, shala...@gmail.com wrote:
>> On Jul 26, 3: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!

>>
>> Use the inline keyword when possible, don't use function pointers,
>> unroll your loops, avoid division if possible, and get Intel's
>> compiler.

>
> ... trying to port your code to run on GPU is also worth a shot.
>


I am confined to what I currently have now.


Reply With Quote
  #30  
Old 07-27-2007, 07:33 AM
Luna Moon
Guest
 
Default Re: C/C++ speed optimization bible/resources/pointers needed!


<jimp@specsol.spam.sux.com> wrote in message
news:1d9on4-6ta.ln1@mail.specsol.com...
> In sci.physics Luna Moon <lunamoonmoon@gmail.com> wrote:
>
>> <jimp@specsol.spam.sux.com> wrote in message
>> news:jtpnn4-ras.ln1@mail.specsol.com...
>> > 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.

>
>> Thanks! But I cannot ask our head to give me a linux box. I am confined
>> within what I have now. I have to do whatever I can and use whatever I
>> have
>> from my part only.

>
> Sigh, Linux is free; all you have to do is download and install it.
>
> Linux will be a LOT faster than Windows when doing number crunching.
>


Who has the proof that Linux is a lot faster than Windows?

I have tried before: in a large organization, Linux is often shared, with
many processes running, everybody has large process to run, I ended up
getting much worse results than Windows.

I cannot build Linux myself, it is a large organization. I am confined.


Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 12:07 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.