Performance penalty for encapsulations ??

Discussion in 'C++' started by Yuri Victorovich, Sep 6, 2003.

  1. 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^0x010101 + 0x437785;
    v2 = i^0x017132 + 0x245726;
    }
    }

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

    // printf("res=%i\n", res.i);
    return (0);
    }
     
    Yuri Victorovich, Sep 6, 2003
    #1
    1. Advertising

  2. 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^0x010101 + 0x437785;
    > v2 = i^0x017132 + 0x245726;
    > }
    > }
    >
    > TYPE res = 0;
    > { // inner product
    > for (int i = 0; i < SZ; i++) {
    > res += v1 * v2;
    > }
    > }
    >
    > // 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.
     
    Kevin Goodsell, Sep 6, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Bär
    Replies:
    2
    Views:
    433
    Yan-Hong Huang[MSFT]
    Jul 18, 2003
  2. Michael Andersson

    Exceptions performance penalty

    Michael Andersson, Sep 2, 2003, in forum: C++
    Replies:
    7
    Views:
    570
    Oliver S.
    Sep 3, 2003
  3. Sune
    Replies:
    2
    Views:
    351
    Martin Wells
    Oct 2, 2007
  4. Rui Maciel

    dereferencing performance penalty?

    Rui Maciel, Jul 27, 2009, in forum: C Programming
    Replies:
    7
    Views:
    1,609
    Antoninus Twink
    Jul 30, 2009
  5. Rui Maciel

    Function pointers: performance penalty?

    Rui Maciel, Oct 11, 2009, in forum: C Programming
    Replies:
    107
    Views:
    4,144
    Sjouke Burry
    Oct 27, 2009
Loading...

Share This Page