delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/03/04/23:01:02

From: Bum-Seok Hyun <dominic AT gong DOT snu DOT ac DOT kr>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: [Q]Computing speed in C++
Date: Thu, 05 Mar 1998 11:52:51 +0900
Organization: Seoul National University, Republic of Korea
Lines: 90
Message-ID: <34FE1383.9EFD3067@gong.snu.ac.kr>
References: <34FCB769 DOT 42BEF1A8 AT gong DOT snu DOT ac DOT kr> <34FD3174 DOT 2401F904 AT gong DOT snu DOT ac DOT kr> <6dk947$kaf AT news-central DOT tiac DOT net>
NNTP-Posting-Host: davinci3.snu.ac.kr
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Martin Ambuhl wrote:

> Bum-Seok Hyun wrote in message <34FD3174 DOT 2401F904 AT gong DOT snu DOT ac DOT kr>...
> |> > So I made a test for comparing computing time.
> |> > The result is
> |> > c code         : 21 sec
> |> > c++ code       : 57 sec
>
> I can't get this kind of difference...
>
> I implemented your test as:
>
> #include <stdio.h>
> #include <time.h>
>
> int main(void)
> {
>     clock_t start = clock();
>     double sum;
> #if defined(__GNUG__) && defined(SLOWMODE)
>    for(int i=1 ; i<10001 ; i++)
>    for(int j=1 ; j<10001 ; j++)
>        for(int k=1 ; k < 11 ;  k++)  sum += i;
> #else
>    int i, j, k;
>    for(i=1 ; i<10001 ; i++)
>    for(j=1 ; j<10001 ; j++)
>        for(k=1 ; k < 11 ;  k++)  sum += i;
> #endif
>    printf("Time = %f\n",(double)(clock()-start)/CLOCKS_PER_SEC);
>    return 0;
> }
>
> Then, using this script:
>
> echo "C compilation..."
> gcc -O2 -m486 a.c -o a.exe
> a
> echo "C++ compilation of C code..."
> gxx -O2 -m486 a.C -o a.exe
> a
> echo "C++ compilation with SLOWMODE..."
> gxx -O2 -m486 -DSLOWMODE a.C -o a.exe
> a
>
> The following output is produced:
>
> C compilation...
> Time = 50.989011
> C++ compilation of C code...
> Time = 51.043956
> C++ compilation with SLOWMODE...
> Time = 54.835165

Hi!. Thanks for your great concern.

I copied your source and that worked just like you wrote.

But, curiosity made me to modify your code.
I divided your code into C version and C++ version.
The differnce is the location of integer i,j,k declaration.

Compile option is
C : gcc -O2 test.c -o test.exe
C++ : gxx -O2 test.cpp -o test.exe

The result is
C version      :  16 sec.
C++ version :  26 sec.
(I ran it on P166 under Windows 95.)

I didn't understand this result so I put a single line like
#if defined(__GNUG__) && defined(SLOWMODE)
     printf("Is C++ alive??? \n");   //  <- this single line
Then, when I compiled using gxx, it showed me that
C++ part in your source code wasn't used.

And another thing is that  I compiled C version using gxx,
the result is almost identical with using gcc.

Anyway, from this ,now I understand variable declarations SHOULD
NOT BE PLACED in nested LOOPs if time is what I need.  :-|

Thank you very much !!!!
------------------------------
Bum-Seok Hyun

mailto:dominic AT gong DOT snu DOT ac DOT kr


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019