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.
|