c++ - is the operator overloading slow down performance? -


im trying make memory pool class , have overload operator[], theres huge(2x) slow down:

  • t(overloaded) = 76.4043 ns
  • t(not-ovld) = 28.6016 ns

is normal or im doing wrong? :)

compiler vc++2013 optimization disabled/full - same thing

class(main.cpp):

template<class t> class pool{ public:     t *cell;     size_t size = 0; public:     pool(const size_t n ){         size = n;         cell = new t[size];     }     t& operator [](const size_t i) { return cell[i]; }     t operator [](const size_t i)const { return cell[i]; } }; 

main(main.cpp):

template<class t> t f( t x){     return x/2 % 100; }  #define test_count 10000000  int main() {     pool<unsigned int> p(test_count);     unsigned int sum = 0;     resettimer();      // test 1     (int = 0; < test_count; i++)         p[i] = f(i);      (int = 0; < test_count; i++)         sum = sum + p[i];      cout << sum << endl;     //     printtimer();     sum = 0;     resettimer();      // test2     (int = 0; < test_count; i++)         p.cell[i] = f(i);      (int = 0; < test_count; i++)         sum = sum + p.cell[i];      cout << sum << endl;     //     printtimer();      int q;     cin >> q;     return 0; } 

problem debug build, in release build (optimization n stuff) works should. hehe stupid mistake taught me :) conclusion - dont measure performence in debug mode ;)


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -