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_in t 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);
}
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_in t 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);
}
Comment