Beginner Question - Good Practice

Started by
14 comments, last by Odion 7 years, 8 months ago

I see. Good to know. I will take it with a pinch of salt. Thank you

Advertisement

This may give you a more realistic idea of the overhead. Keep in mind that testing should be done in release build without a debugger attached. When viewing the results consider the number of iterations in comparison to the number of allocations/releases in your program. (100 million is a lot.)


#include <iostream>
#include <memory>
#include <chrono>

//this templated struct allows you to create POD (plain old data) objects of arbitrary size
template<size_t SIZE>
struct POD {
  char doota[SIZE];
};

//same idea but with some busywork constructor and destructors
template<size_t SIZE>
class Complex {
public:
  Complex() {
    for(size_t i = 0; i < SIZE; i++) { doota[i] = 1; }
  }

  ~Complex() {
    for(size_t i = 0; i < SIZE; i++) { doota[i] = 0; }
  }

private:
  char doota[SIZE];
};

//here we define some terms for convenience. this use of 'using' is similar to typedef, but easier to read and it works for templates
const size_t LARGE = 1024;
const size_t SMALL = 1;
using LargePOD = POD<LARGE>;
using SmallPOD = POD<SMALL>;
using LargeComplex = Complex<LARGE>;
using SmallComplex = Complex<SMALL>;

//this is a template function that uses manual memory management to create
// and delete 'iterations' number of objects for type T (the type is passed
// in as a template argument at the call site)
template<class T>
void manualMMTest(size_t iterations) {
  for(size_t i = 0; i < iterations; i++) {
    delete new T;
  }
}

//same thing using unique_ptr
template<class T>
void uniqPtrMMTest(size_t iterations) {
  for(size_t i = 0; i < iterations; i++) {
    std::make_unique<T>();
  }
}

//and shared_ptr
template<class T>
void sharPtrMMTest(size_t iterations) {
  for(size_t i = 0; i < iterations; i++) {
    std::make_shared<T>();
  }
}

//this is a high-precision timer class that should be
//fairly simple to understand by reading it
class Timer {
  using moment = std::chrono::time_point<std::chrono::high_resolution_clock>;

public:
  void startFromZero() {
    start = std::chrono::high_resolution_clock::now();
  }

  double getElapsedSeconds() {
    moment stop = std::chrono::high_resolution_clock::now();
    std::chrono::nanoseconds elapsed = stop - start;
    return elapsed.count() / 1000000000.0;
  }
  
private:
  moment start;
};

//this runs a batch of tests using the previously defined functions and prints the results
template<class T>
void doBatch(size_t iterations) {
  Timer timer;
  double seconds = 0;

  timer.startFromZero();
  manualMMTest<T>(iterations);
  seconds = timer.getElapsedSeconds();
  std::cout << "  Manual management: " << seconds << " seconds.\n";

  timer.startFromZero();
  uniqPtrMMTest<T>(iterations);
  seconds = timer.getElapsedSeconds();
  std::cout << "  unique_ptr: " << seconds << " seconds.\n";

  timer.startFromZero();
  sharPtrMMTest<T>(iterations);
  seconds = timer.getElapsedSeconds();
  std::cout << "  shared_ptr: " << seconds << " seconds.\n\n";
}

//and this runs it all
int main() {
  const size_t ITERATIONS = 100000000;

  std::cout << "Begin testing on int (using " << ITERATIONS << " iterations):\n";
  doBatch<int>(ITERATIONS);

  std::cout << "Begin testing on small POD objects (using " << ITERATIONS << " iterations):\n";
  doBatch<SmallPOD>(ITERATIONS);

  std::cout << "Begin testing on large POD objects (using " << ITERATIONS << " iterations):\n";
  doBatch<LargePOD>(ITERATIONS);

  std::cout << "Begin testing on small complex objects (using " << ITERATIONS << " iterations):\n";
  doBatch<SmallComplex>(ITERATIONS);

  std::cout << "Begin testing on large complex objects (using " << ITERATIONS << " iterations):\n";
  doBatch<LargeComplex>(ITERATIONS);


  std::cin.get();
}


void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Sorry I have no idea what this is. Can you elaborate please?

Added comments.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Clearer. Thanks. It says build is completed, but there is no exe file anywhere....its too late now, will look into it tomorrow

Wow! Nice program! Nice test. According to this test shared pointers are definitely not 8-9 times slower.

This topic is closed to new replies.

Advertisement