Slow C++?

D

dgront

Have a look on a two pieces of C and C++ code, doing the same:

Version in C++:
class Point {
public:
double x;
double y;
double z;
};

vector<Point> v;
int n = 3000; // n is also the size of v
for(int j=1;j<n;j++) {
for(i=c.begin();i!=c.end();++i) {
i->x*=j;
i->y*=j;
i->z*=j;
}
for(i=c.begin();i!=c.end();++i) {
i->x/=j;
i->y/=j;
i->z/=j;
}
}
.... and in pure C
int *x,*y,*z; // Arrays have size n
for(int j=1;j<n;j++) {
for(int i=0;i!=n;++i) {
x*=j;
y*=j;
z*=j;
}
for(int i=0;i!=n;++i) {
x/=j;
y/=j;
z/=j;
}
}

The second one is 10 times faster (gcc version 3.2.2 20030222 (Red Hat
Linux 3.2.2-5), -O3)
Does it have to be so, or maybe I missed something?

Dominik Gront
 
E

E. Robert Tisdale

Dominik said:
Have a look on a two pieces of C and C++ code, doing the same:

Version in C++:
class Point {
public:
double x;
double y;
double z;
};

vector<Point> v;
int n = 3000; // n is also the size of v
for(int j=1;j<n;j++) {
for(i=c.begin();i!=c.end();++i) {
i->x*=j;
i->y*=j;
i->z*=j;
}
for(i=c.begin();i!=c.end();++i) {
i->x/=j;
i->y/=j;
i->z/=j;
}
}
... and in pure C
int *x,*y,*z; // Arrays have size n
for(int j=1;j<n;j++) {
for(int i=0;i!=n;++i) {
x*=j;
y*=j;
z*=j;
}
for(int i=0;i!=n;++i) {
x/=j;
y/=j;
z/=j;
}
}

The second one is 10 times faster
(gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5), -O3)
Does it have to be so, or maybe I missed something?


This us an obvious troll.
Please ignore it.
 
A

Andrew Koenig

Have a look on a two pieces of C and C++ code, doing the same:

Version in C++:
class Point {
public:
double x;
double y;
double z;
};

vector<Point> v;
int n = 3000; // n is also the size of v
for(int j=1;j<n;j++) {
for(i=c.begin();i!=c.end();++i) {
i->x*=j;
i->y*=j;
i->z*=j;
}
for(i=c.begin();i!=c.end();++i) {
i->x/=j;
i->y/=j;
i->z/=j;
}
}
... and in pure C
int *x,*y,*z; // Arrays have size n
for(int j=1;j<n;j++) {
for(int i=0;i!=n;++i) {
x*=j;
y*=j;
z*=j;
}
for(int i=0;i!=n;++i) {
x/=j;
y/=j;
z/=j;
}
}

The second one is 10 times faster (gcc version 3.2.2 20030222 (Red Hat
Linux 3.2.2-5), -O3)
Does it have to be so, or maybe I missed something?


Well, for starters, the two programs don't do the same thing. The C version
should really have been something like

Point *x;

for (int i = 0; i != n; ++i) {
x.x *= j;
x.y *= j;
x.z *= j;
}

and so on.

Nevertheless, I find the speed difference surprising, and wonder if there
isn't something else going on that it might be possible to see if you showed
us the entire program and explained your measurement methodology.
 
V

Victor Bazarov

dgront said:
Have a look on a two pieces of C and C++ code, doing the same:

Version in C++:
class Point {
public:
double x;
double y;
double z;
};

vector<Point> v;
int n = 3000; // n is also the size of v
for(int j=1;j<n;j++) {
for(i=c.begin();i!=c.end();++i) {
i->x*=j;
i->y*=j;
i->z*=j;
}
for(i=c.begin();i!=c.end();++i) {
i->x/=j;
i->y/=j;
i->z/=j;
}
}
... and in pure C
int *x,*y,*z; // Arrays have size n
for(int j=1;j<n;j++) {
for(int i=0;i!=n;++i) {
x*=j;
y*=j;
z*=j;
}
for(int i=0;i!=n;++i) {
x/=j;
y/=j;
z/=j;
}
}

The second one is 10 times faster (gcc version 3.2.2 20030222 (Red Hat
Linux 3.2.2-5), -O3)
Does it have to be so, or maybe I missed something?


I am not sure what you missed. Here is the complete program and
its output:
---------------------------------------------------
#include <vector>
#include <iostream>
#include <ctime>
using namespace std;

class Point {
public:
Point() : x(0),y(0),z(0) {}
double x;
double y;
double z;
};

int main()
{
int n = 3000; // n is also the size of v
vector<Point> v(3000);
clock_t c1 = clock();
for(int j=1;j<n;j++) {
vector<Point>::iterator i, e=v.end();
for(i=v.begin();i!=e;++i) {
i->x*=j;
i->y*=j;
i->z*=j;
}
for(i=v.begin();i!=e;++i) {
i->x/=j;
i->y/=j;
i->z/=j;
}
}
clock_t c2 = clock();

int x[3000] = {0}, y[3000] = {0}, z[3000] = {0};
clock_t c3 = clock();
for(int j=1;j<n;j++) {
for(int i=0;i!=n;++i) {
x*=j;
y*=j;
z*=j;
}
for(int i=0;i!=n;++i) {
x/=j;
y/=j;
z/=j;
}
}
clock_t c4 = clock();

cout << "Vector : " << c2 - c1
<< ", whereas arrays : " << c4 - c3 << endl;
}
------------------------------------- Debug version (VC++ v7.1):
Vector : 2155, whereas arrays : 422
------------------------------------- Release version (VC++ v7.1):
Vector : 78, whereas arrays : 656
-------------------------------------------------------
(HP Compaq D530 Pentium 4 HT 2.6 GHz)

Try explaining that... Probably G++ optimizer is really dumb.

V
 
M

majere

Victor Bazarov said:
------------------------------------- Debug version (VC++ v7.1):
Vector : 2155, whereas arrays : 422
------------------------------------- Release version (VC++ v7.1):
Vector : 78, whereas arrays : 656

Well, on a slower machine, but with G++ 3.4 on linux:

No optimizations:

Vector : 1140000, whereas arrays : 830000

With -O3:

Vector : 500000, whereas arrays : 1050000


I do find it strange that the array case was slower when compiled
with -O3...
 
E

E. Robert Tisdale

Victor said:
dgront said:
Have a look on a two pieces of C and C++ code, doing the same:

Version in C++:
class Point {
public:
double x;
double y;
double z;
};

vector<Point> v;
int n = 3000; // n is also the size of v
for(int j=1;j<n;j++) {
for(i=c.begin();i!=c.end();++i) {
i->x*=j;
i->y*=j;
i->z*=j;
}
for(i=c.begin();i!=c.end();++i) {
i->x/=j;
i->y/=j;
i->z/=j;
}
}
... and in pure C
int *x,*y,*z; // Arrays have size n
for(int j=1;j<n;j++) {
for(int i=0;i!=n;++i) {
x*=j;
y*=j;
z*=j;
}
for(int i=0;i!=n;++i) {
x/=j;
y/=j;
z/=j;
}
}

The second one is 10 times faster (gcc version 3.2.2 20030222
(Red Hat Linux 3.2.2-5), -O3)
Does it have to be so, or maybe I missed something?


I am not sure what you missed.
Here is the complete program and its output:

cat main.cc
#include <vector>
#include <iostream>
#include <ctime>

class Point {
public:
Point(void): x(0), y(0), z(0) { }
double x;
double y;
double z;
};

int main(int argc, char* argv[]) {
const int n = 3000; // n is also the size of v
std::vector<Point> v(3000);
clock_t c1 = clock();
for (int j = 1; j < n; ++j) {
typedef std::vector<Point>::iterator iterator;
for (iterator i = v.begin(); i != v.end(); ++i) {
i->x *= j;
i->y *= j;
i->z *= j;
}
for (iterator i = v.begin(); i != v.end(); ++i) {
i->x /= j;
i->y /= j;
i->z /= j;
}
}
clock_t c2 = clock();

int x[3000] = {0}, y[3000] = {0}, z[3000] = {0};
clock_t c3 = clock();
for (int j = 1; j < n; ++j) {
for (int i = 0; i != n; ++i) {
x *= j;
y *= j;
z *= j;
}
for (int i = 0; i != n; ++i) {
x /= j;
y /= j;
z /= j;
}
}
clock_t c4 = clock();

std::cout << "Vector : " << c2 - c1
<< ", whereas arrays : " << c4 - c3 << std::endl;
return 0;
}
g++ -Wall -ansi -pedantic -O3 -o main main.cc
time ./main
Vector : 970000, whereas arrays : 1960000
2.935u 0.004s 0:02.94 99.6% 0+0k 0+0io 0pf+0w
g++ --version g++ (GCC) 3.4.1
cat /etc/redhat-release
Red Hat Linux release 8.0 (Psyche)
 
R

rossum

Have a look on a two pieces of C and C++ code, doing the same:
You are incorrect, they are not doing the same things, they are doing
different things.
Version in C++:
class Point {
public:
double x;
double y;
double z;
};
Use of double noted.

[snip]

... and in pure C
int *x,*y,*z; // Arrays have size n
Use of int noted.

[snip]
The second one is 10 times faster (gcc version 3.2.2 20030222 (Red Hat
Linux 3.2.2-5), -O3)
Does it have to be so, or maybe I missed something?
Yes, you have missed the fact that the two tests are not testing the
same thing. The C++ test uses doubles and the C test uses ints. Very
unobservant of you. Since you are not comparing like with like, your
results are of very little interest.

rossum
 
A

Andrey Tarasevich

dgront said:
Have a look on a two pieces of C and C++ code, doing the same:

They are not doing the same. The first one (C++) performs floating-point
computations. In the second one (C) the computations are strictly integral.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,279
Messages
2,571,387
Members
48,090
Latest member
marky2025

Latest Threads

Top