Re: 2nd Attempt : How can I count the actual number of operations performed in a program
posted:
ok I know I posted this question previously and then I got a reply and
I realized that I had asked a really dumb question but now I realize
that my question wasn't that dumb at all !
Here's the route I'd take. (And yes it's dirty). Here comes some unchecked,
off-the-cuff code:
class Counter {
static unsigned long total_amount; // <- Total amount for all types
unsigned long specific_amount; // <- Total amount for a specific type
public:
Counter() : specific_amount(0) {}
Counter& operator++() { ++total_amount; ++specific_amount; return *this;
}
static unsigned long GetTotalAmount() { return total_amount; }
unsigned long GetAmount() const { return specific_amount; }
};
unsigned long Counter::total_amount = 0;
template<class T>
class PrimitiveTypeHolder {
private:
T value;
public:
static Counter counter;
T& operator+=(const T& rhs)
{
++counter; /* Record the operation */
value += rhs;
return *this;
}
T operator+(const T& rhs)
{
return PrimitiveTypeHolder<T>(*this).operator+=(rhs);
}
};
Then, I would use macro trickery to turn:
int a;
float b;
double c;
into:
PrimitiveTypeHolder<int> a;
PrimitiveTypeHolder<float> b;
PrimitiveTypeHolder<double> c;
I'm not sure if the following macro would work:
#define int PrimitiveTypeHolder<int>
And then at the end of your program, you can check the total amount of
operations:
cout << Counter::GetTotalAmount();
Or just for a given type:
cout << PrimitiveTypeHolder<float>().counter.GetAmount();
There's probably a thousand ways of doing this, but this one came to mind.
-Tom1s