473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Slow C++?

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
7 1201
"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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: yawnmoth | last post by:
using the gethostbyname function seems to noticeably slow down pages. some of the comments in php.net's gethostbyname entry suggest using a version that caches the result, but those versions also...
8
by: Neil | last post by:
I have a very puzzling situation with a database. It's an Access 2000 mdb with a SQL 7 back end, with forms bound using ODBC linked tables. At our remote location (accessed via a T1 line) the time...
2
by: David | last post by:
Hi, We have an internal network of 3 users. Myself & one other currently have individual copies of the front-end MS Access forms and via our individual ODBC links we have used the: File > Get...
3
by: Jennyfer J Barco | last post by:
In my application I have a datagrid. The code calls a Stored procedure and brings like 200 records. I created a dataset and then a dataview to bind the results of the query to my grid using ...
50
by: diffuser78 | last post by:
I have just started to learn python. Some said that its slow. Can somebody pin point the issue. Thans
0
by: Pratchaya | last post by:
In my.cnf i add these lines ####### log-bin log-slow-queries = /var/log/mysqld-slow.log long_query_time=1 #######
2
by: mezise | last post by:
Posted by Pratchaya: ------------------------------------------------------ MySQL Slow Log ERROR In my.cnf i add these lines ####### log-bin log-slow-queries = /var/log/mysqld-slow.log
13
by: eighthman11 | last post by:
using Access 2003 and sql server version 8.0 Hey everyone. Created a text box where the user types in an Inventory number and it takes them to that inventory number on the contimuous form. The...
3
by: John | last post by:
Hi I have replaced an ms access app with its vb.net version at a client site. Now the clients keeps complaining about how slow the app response is. The complains they have are for example when...
10
by: penworthamnaynesh | last post by:
Does php slow your website down? This is what i would like to know.The reason is because my site is writtent 50% in html and 50% in php and is very slow at loading. And i cant tell wether php is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.