Message from discussion
Why C is much slower than Fortran
From: Vincent Ma <vinm...@hotmail.com>
Subject: Re: Why C is much slower than Fortran
Date: 1999/04/12
Message-ID: <37122569.BF79CD19@hotmail.com>#1/1
X-Deja-AN: 465531438
Content-Transfer-Encoding: 7bit
References: <3710584B.1C0F05F5@hotmail.com> <7esvkr$v8g$1@camel29.mindspring.com>
To: "Peter S. Shenkin" <shen...@schrodinger.com>
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
Organization: University of California, Los Angeles
Mime-Version: 1.0
Newsgroups: comp.lang.c++,comp.lang.fortran,comp.compiler
Dear friends,
Thanks for the reply. Here are more detailed information:
-------------------------------------------------------------
#include <stdio.h>
void main()
{
float E[36][21][52];
float H1[36][21][52];
float H2[36][21][52];
short int ID[43][31][52];
float A1[10];
float A2[10];
float A3[10];
int i,j,k,nlp,nmax;
int imax, jmax, kmax;
/* ============= set parameters ============== */
imax = 35;
jmax = 20;
kmax = 50;
/* ============== input nmax ================= */
nmax = 1000;
/* ----------- set initial conditions ---------- */
for(k = 1; k <= kmax-1; k++)
{
for(j = 1; j <= jmax-1; j++)
{
for(i = 1; i <= imax-1; i++)
{
ID[i][j][k] = 0;
E[i][j][k] = 4.;
H1[i][j][k] = 5.;
H2[i][j][k] = 6.;
A1[0] = 0.;
A2[0] = 2.;
A3[0] = 3.;
}
}
}
/* ############################## */
for (nlp = 0; nlp <= nmax; nlp++)
{
for(i = 0; i <= imax-1; i++)
{
for(j = 1; j <= jmax-1; j++)
{
for(k = 1; k <= kmax-1; k++)
{
E[i][j][k] = A1[ID[i][j][k]]*E[i][j][k] + A2[ID[i][j][k]]
* (H1[i][j][k] - H1[i][j-1][k] + A3[ID[i][j][k]] * (H2[i][j][k] - H2[i][j][k-1]
);
}
}
}
}
return;
}
-----------------------------------------------------------------------------------------------------------------------
Fortran test code is same. I ran both on Pentium 133 with 64 meg RAM with MS
Powerstation and Visual C++ 5 with full optimization on. The execution time are
Fortran -- 11 sec (i loop innermost)
C -- 101 sec (i loop innermost)
C -- 62 sec (k innermost).
The difference is still a lot (6 times). I've tried many variations, it seems no
matter what I did, C is always slower. My previous experience on workstation
told me the same story that fortran is much faster than C (at least for this
scientific computing case).