[Bug c++/14809] New: performance depends on allocation methods
boeck at fhi-berlin dot mpg dot de
gcc-bugzilla@gcc.gnu.org
Thu Apr 1 12:06:00 GMT 2004
Compiler: gcc 3.3.3
System: Redhat, SuSE 8.1, 8.2, 9.0, Pentium3, 1.2GHz, 2GB Ram
compiler options: default options, was compiled with "configure, make, make install"
command line: g++ -O2 file.cpp
compiler output: no warning, no error
output of "g++ -v":
Reading specs from /usr/local/gcc-3.3.3/lib/gcc-lib/i686-pc-linux-gnu/3.3.3/specs
Configured with: /soft/gcc/gcc-3.3.3/gcc-3.3.3/configure --prefix=/usr/local/gcc-3.3.3
--enable-shared --enable-languages=c,c++,f77
Thread model: posix
gcc version 3.3.3
The performance of a program seems to depend on the way how vectors are allocated:
Here are 4 small example codes:
(a) slow[35sec]: using C++ "new/delete"
(b) fast[22sec]: using C-like malloc
(c) slow[35sec]: using C-like malloc via function call
(d) fast[22sec]: using C-like malloc via INLINE fuction call
g++ -O2 [a,b,c,d].cpp
timex a.out
i don't understand, why the performance depends on the way how i allocate pointers.
Please help.
thanks
Sixten
----- a.cpp ---
#include <stdio.h>
int main ()
{
long it;
int i, n = 1000;
double *x = new double [n];
double *y = new double [n];
double a = 0.2, b = -a;
for (i=0; i < n; ++i) x[i] = y[i] = i;
for (it=0; it < 5000000L; ++it) {
for (i=0; i < n; ++i) {
y[i] += a * x[i];
y[i] += b * x[i];
}
}
printf ("%g\n", y[0]);
delete [] y;
delete [] x;
}
---- b.cpp ---
int main ()
{
long it;
int i, n = 1000;
double *x = (double *)malloc ((size_t)(n * sizeof(double)));
double *y = (double *)malloc ((size_t)(n * sizeof(double)));
double a = 0.2, b = -a;
for (i=0; i < n; ++i) x[i] = y[i] = i;
for (it=0; it < 5000000L; ++it) {
for (i=0; i < n; ++i) {
y[i] += a * x[i];
y[i] += b * x[i];
}
}
printf ("%g\n", y[0]);
free (y);
free (x);
}
--- c.cpp ---
double *myMalloc (int nElem)
{
return (double *)(malloc ((size_t)(nElem * sizeof(double))));
}
int main ()
{
long it;
int i, n = 1000;
double *x = myMalloc(n);
double *y = myMalloc (n);
double a = 0.2, b = -a;
for (i=0; i < n; ++i) x[i] = y[i] = i;
for (it=0; it < 5000000L; ++it) {
for (i=0; i < n; ++i) {
y[i] += a * x[i];
y[i] += b * x[i];
}
}
printf ("%g\n", y[0]);
free (y);
free (x);
}
--- d.cpp ---
inline double *myMalloc (int nElem)
{
return (double *)(malloc ((size_t)(nElem * sizeof(double))));
}
int main ()
{
long it;
int i, n = 1000;
double *x = myMalloc(n);
double *y = myMalloc (n);
double a = 0.2, b = -a;
for (i=0; i < n; ++i) x[i] = y[i] = i;
for (it=0; it < 5000000L; ++it) {
for (i=0; i < n; ++i) {
y[i] += a * x[i];
y[i] += b * x[i];
}
}
printf ("%g\n", y[0]);
free (y);
free (x);
}
--
Summary: performance depends on allocation methods
Product: gcc
Version: 3.3.3
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: boeck at fhi-berlin dot mpg dot de
CC: gcc-bugs at gcc dot gnu dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14809
More information about the Gcc-bugs
mailing list