Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Performance penalty for encapsulations ??

Reply
Thread Tools

Performance penalty for encapsulations ??

 
 
Yuri Victorovich
Guest
Posts: n/a
 
      09-06-2003
I have simple program which computes internal
product on large vector.
Whan the element of vector is "int" performance is
about 70% better than when I use class with single
field "int" in it and overload appropriate operators.

Does this indicate wakness of compiler or this is natural
for C++ ?

I compared with gcc-3.2 with -O3 optimization.

Yuri

----------code-----------------------------------------

#include <stdio.h>

class C_int {
public:
int i;
inline C_int() { }
inline C_int(int new_i) { i = new_i; }
inline C_int operator*(C_int i1) {
C_int ii(i*i1.i);
return (ii);
}
inline C_int operator+(C_int i1) {
C_int ii(i+i1.i);
return (ii);
}
inline void operator+=(C_int i1) {
i += i1.i;
}
inline void operator=(int ii) { i = ii; }
// inline void operator(int)() { return (i); }
};

#define TYPE C_int // 1
//#define TYPE C_int // 2
#define SZ 30000000

TYPE v1[SZ];
TYPE v2[SZ];

int
main(int argc, const char *argv[]) {

{ // initialize
for (int i = 0; i < SZ; i++) {
v1[i] = i^0x010101 + 0x437785;
v2[i] = i^0x017132 + 0x245726;
}
}

TYPE res = 0;
{ // inner product
for (int i = 0; i < SZ; i++) {
res += v1[i] * v2[i];
}
}

// printf("res=%i\n", res.i);
return (0);
}
 
Reply With Quote
 
 
 
 
Kevin Goodsell
Guest
Posts: n/a
 
      09-06-2003
Yuri Victorovich wrote:

> I have simple program which computes internal
> product on large vector.
> Whan the element of vector is "int" performance is
> about 70% better than when I use class with single
> field "int" in it and overload appropriate operators.
>
> Does this indicate wakness of compiler or this is natural
> for C++ ?
>


There are a lot of possible contributors to the performance decrease. I
don't know how you are measuring the time, but note that you are
constructing 30000000 class instances, which is obviously going to take
some time. Did you include that in your timing?

Another likely issue is that you've reduced the compiler's ability to
optimize.

You can create pretty significant optimizations by using classes well,
but if you use them poorly all you do is complicate the compiler's job,
likely making the code less efficient.

> I compared with gcc-3.2 with -O3 optimization.
>
> Yuri
>
> ----------code-----------------------------------------
>
> #include <stdio.h>
>
> class C_int {
> public:
> int i;
> inline C_int() { }


You don't need to say 'inline' here. Functions defined within the class
definition are always inlined.

> inline C_int(int new_i) { i = new_i; }
> inline C_int operator*(C_int i1) {
> C_int ii(i*i1.i);
> return (ii);
> }
> inline C_int operator+(C_int i1) {
> C_int ii(i+i1.i);
> return (ii);
> }
> inline void operator+=(C_int i1) {
> i += i1.i;
> }
> inline void operator=(int ii) { i = ii; }
> // inline void operator(int)() { return (i); }


If that was supposed to be a conversion to int you got it wrong.

operator int() { return i; }

> };
>
> #define TYPE C_int // 1
> //#define TYPE C_int // 2
> #define SZ 30000000


Why macros? Macros are bad. Use typedef and const instead.

>
> TYPE v1[SZ];
> TYPE v2[SZ];
>
> int
> main(int argc, const char *argv[]) {


I don't think this is a legal signature for main. According to the
standard, the second argument must be 'char *argv[]' or something
equivalent. I don't think adding 'const' is allowed.

>
> { // initialize


What's with the extra redundant blocks?

> for (int i = 0; i < SZ; i++) {
> v1[i] = i^0x010101 + 0x437785;
> v2[i] = i^0x017132 + 0x245726;
> }
> }
>
> TYPE res = 0;
> { // inner product
> for (int i = 0; i < SZ; i++) {
> res += v1[i] * v2[i];
> }
> }
>
> // printf("res=%i\n", res.i);
> return (0);
> }


-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function pointers: performance penalty? Rui Maciel C Programming 107 10-27-2009 09:27 PM
dereferencing performance penalty? Rui Maciel C Programming 7 07-30-2009 06:06 PM
Integer promotions and performance penalty Sune C Programming 2 10-02-2007 11:06 AM
Exceptions performance penalty Michael Andersson C++ 7 09-03-2003 04:39 PM
Is there Performance Penalty for multiple cross-assembly-calls? Peter Bär ASP .Net 2 07-18-2003 03:17 AM



Advertisments