By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,006 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

Slow C++?

P: n/a
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[i]*=j;
y[i]*=j;
z[i]*=j;
}
for(int i=0;i!=n;++i) {
x[i]/=j;
y[i]/=j;
z[i]/=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

Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"dgront" <dg****@chem.uw.edu.pl> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
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[i]*=j;
y[i]*=j;
z[i]*=j;
}
for(int i=0;i!=n;++i) {
x[i]/=j;
y[i]/=j;
z[i]/=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[i].x *= j;
x[i].y *= j;
x[i].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.
Jul 23 '05 #2

P: n/a
Dominik Gront wrote:
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[i]*=j;
y[i]*=j;
z[i]*=j;
}
for(int i=0;i!=n;++i) {
x[i]/=j;
y[i]/=j;
z[i]/=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.
Jul 23 '05 #3

P: n/a
dgront wrote:
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[i]*=j;
y[i]*=j;
z[i]*=j;
}
for(int i=0;i!=n;++i) {
x[i]/=j;
y[i]/=j;
z[i]/=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[i]*=j;
y[i]*=j;
z[i]*=j;
}
for(int i=0;i!=n;++i) {
x[i]/=j;
y[i]/=j;
z[i]/=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
Jul 23 '05 #4

P: n/a
Victor Bazarov <v.********@comAcast.net> writes:
------------------------------------- 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.


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...
Jul 23 '05 #5

P: n/a
On 16 Feb 2005 13:37:39 -0800, "dgront" <dg****@chem.uw.edu.pl> wrote:
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

Dominik Gront

--

The ultimate truth is that there is no Ultimate Truth
Jul 23 '05 #6

P: n/a
Victor Bazarov wrote:
dgront wrote:
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[i]*=j;
y[i]*=j;
z[i]*=j;
}
for(int i=0;i!=n;++i) {
x[i]/=j;
y[i]/=j;
z[i]/=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[i] *= j;
y[i] *= j;
z[i] *= j;
}
for (int i = 0; i != n; ++i) {
x[i] /= j;
y[i] /= j;
z[i] /= 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) cat /proc/cpuinfo

Jul 23 '05 #7

P: n/a
dgront wrote:
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.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.