[Novalug] C compilers

Varol Okan varokan@movingsatellites.com
Sat Apr 10 19:30:04 EDT 2010


As I said before it is really great that the startup of a c program is
some milliseconds faster.

Try the following though

#include <stdio.h>
static long g_iNumberOfRects = 1000000000;
int main ( int argc, char *argv[] )
{
double fMid, fHeight, fWidth, fArea, fSum = 0.0;
long t;
fWidth = 1.0 / (double)g_iNumberOfRects;
for ( t=0; t<g_iNumberOfRects; t++ ) {
fMid = ( 0.5 + t ) * fWidth;
fHeight = 4.0 /( 1.0 + fMid * fMid );
fSum += fHeight;
}
fArea = fWidth * fSum;
printf ( "The value of PI is %.32f\n\n", fArea );
return 0;
}

> time ./hello_world_cpp
The value of PI is 3.14159265359214012747202104947064
real 0m23.385s
user 0m21.805s
sys 0m0.048s

> time ./hello_world_c
The value of PI is 3.14159265359214012747202104947064
real 0m23.652s
user 0m21.669s
sys 0m0.104s

This was a one time run though you see the difference is within the
margin of error ( particular considering system caching ).
It does not matter whether you use C or C++, the real performance boost
comes from the gray matter in a programmers / engineers head who can
refactor this routine ( without falling back to assembler ) by using
multiple threads/processes/hosts or come up with some other clever way
of calculating PI.

I am not saying I am an expert in these things, I am however an expert
when it comes to real world code, especially legacy code from 'superstar
programmers'. And as such I can say that it does not matter at all if
you use a C or C++ compiler. After all what you want is a program that
does something and unless you get all people who write code to truly
optimize the hell out of their programs, it makes no difference in
99.984566456% +/- 2^-9 of the code out there.

Varol :)

Walt Smith wrote:
> Hi,
>
> Generally, I don't "do optimization" since it can be
> tedious in a number of ways.  Give me an app with a real need,
> and I respond to a specific real need.
>
> so, just for discussion sake,
> I did something simple..
>
> I wrote a "hello world".
>
> starting backward, since results are the "best" part,
> the timing is:
>
> C code      CPP code
> 8  1  5      11 33 7
>
> in 3 runs, by simply adding results.
>
> $time( hello_c)
> $time( hello_cpp)
>
> groaty setup of the code:
>
> of the following identical code stored as 2 files
> hello_world.c and hello_world.cpp:
> ----------
> #include <stdio.h>
>
> int main(){
>    printf("Hellow World\n");
>    return(0);
> }
> ------------
> and compiled as:
> gcc -Wall hello_world.c    -o hello_world_c
> g++ -Wall hello_world.cpp  -o hello_world_cpp
>
>
> ----
> (I'm not ignoring yet the desire for a try/catch.
> Except I figure that function probably requires some
> amount of built-in OOP, which sounds like a lot of work
> and/or overhead.)  I did examine and see that gcc will do
> objective C code ( which I did not know until pointed
> out on this list) and that Objective-C has some kind of try/catch. 
>
>
> Walt.....
>
>
>
>       
> _______________________________________________
> Novalug mailing list
> Novalug@calypso.tux.org
> http://calypso.tux.org/mailman/listinfo/novalug
>   




More information about the Novalug mailing list