GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: Add Line Numbers


Quiz Answer by glDaher
======
ANSWER
======
The first one is faster. Run this program:


#include <iostream>


class Int
    {
    public:
        Int()
            {
            Int::constuction ++;
            }
        Int(int i)
            {
            Int::constuction ++;
            *this = i;
            }
        ~Int()
            {
            Int::destruction ++;
            }

        Int& operator ++(int)
            {
            this->val++;
            this->increments ++;
            return *this;
            }
        Int& operator =(int left)
            {
            this->val = left;
            this->eq ++;
            return *this;
            }

        bool operator < (int left)
            {
            Int::comparisons ++;
            return this->val < left;
            }

    private: int val;

    public:    static int comparisons;
            static int increments;
            static int constuction;
            static int destruction;
            static int eq;
    };

int Int::comparisons = 0;
int Int::increments  = 0;
int Int::constuction = 0;
int Int::destruction = 0;
int Int::eq          = 0;


void main()
    {    
        {
        for(Int i=0; i<50; i++)
            for( Int j=0; j<100; j++)
                ;
        }

    std::cout << "comparisons = " << Int::comparisons << std::endl;
    std::cout << "increments = " << Int::increments << std::endl;
    std::cout << "constuction = " << Int::constuction << std::endl;
    std::cout << "destruction = " << Int::destruction << std::endl;
    std::cout << "eq = " << Int::eq << std::endl;

    std::cout << "==========================\n\n";

    Int::comparisons = 0;
    Int::increments  = 0;
    Int::constuction = 0;
    Int::destruction = 0;
    Int::eq          = 0;

        {
        for(Int i=0; i<100; i++)
            for( Int j=0; j<50; j++)
                ;
        }

    std::cout << "comparisons = " << Int::comparisons << std::endl;
    std::cout << "increments = " << Int::increments << std::endl;
    std::cout << "constuction = " << Int::constuction << std::endl;
    std::cout << "destruction = " << Int::destruction << std::endl;
    std::cout << "eq = " << Int::eq << std::endl;
    }