445,898 Members | 2,006 Online
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 v; int n = 3000; // n is also the size of v for(int j=1;jx*=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
7 Replies

 P: n/a "dgront" 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 v; int n = 3000; // n is also the size of v for(int j=1;jx*=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

 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 v; int n = 3000; // n is also the size of v for(int j=1;jx*=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

 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 v; int n = 3000; // n is also the size of v for(int j=1;jx*=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 #include #include 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 v(3000); clock_t c1 = clock(); for(int j=1;j::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

 P: n/a Victor Bazarov 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" 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 Cint *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 HatLinux 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 v; int n = 3000; // n is also the size of v for(int j=1;jx*=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 #include #include 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 v(3000); clock_t c1 = clock(); for (int j = 1; j < n; ++j) { typedef std::vector::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.