Shaving some time

May 24, 2010 by · 3 Comments
Filed under: Development, Linux 

I was talking to a colleague the other day and got into a discussion regarding some ways of coding c++, such as whether to use division or bit shifting — almost all modern  compilers can optimize this so it’s basically just a matter of what looks best. At the same time, we got into a minor discussion regarding references and pointers in c++. I made a small test and found some rather amusing results, which is quite obvious once you think about it, but still very scary considering how common it is to use the pointer construct:

// Compile using g++ -lrt -o lala lala.cpp
#include <iostream>
#include <time.h>

int* lala_ptr()
{
    int *y = new int;
    *y = 5;
    return y;
}

int& lala_ref()
{
    int x =5;
    int &y = x;
    return y;
}

timespec diff(timespec start, timespec end)
{
    timespec temp;
    if ((end.tv_nsec-start.tv_nsec)<0) {
        temp.tv_sec = end.tv_sec-start.tv_sec-1;
        temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
    } else {
        temp.tv_sec = end.tv_sec-start.tv_sec;
        temp.tv_nsec = end.tv_nsec-start.tv_nsec;
    }
    return temp;
}

int main(int argc, char **argv)
{
    timespec time1, time2, time3;

    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
    for (unsigned int i=0;i<(unsigned int)-1;i++)
    {
        int &z = lala_ref();
    }
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
    for (unsigned int i=0;i<(unsigned int)-1;i++)
    {
        int *z = lala_ptr();
        delete z;
    }
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time3);

    std::cout << "Reference diff(" << time1.tv_sec << ":" << 
                time1.tv_nsec << ", " << 
                time2.tv_sec << ":" << 
                time2.tv_nsec << ") = " << 
                diff(time1, time2).tv_sec << ":" << 
                diff(time1, time2).tv_nsec << std::endl;

    std::cout << "Pointer diff(" << time2.tv_sec << ":" << 
                time2.tv_nsec << ", " << 
                time3.tv_sec << ":" << 
                time3.tv_nsec << ") = " <<
                diff(time2, time3).tv_sec << ":" << 
                diff(time2, time3).tv_nsec << std::endl;
}

Below is a sample of the output generated by the test code above:

oan@laptop4:~/Projects/test$ ./testRefVsPointer 
Reference diff(0:3869272, 25:234466470) = 25:230597198
Pointer diff(25:234466470, 299:547382527) = 274:312916057

So, the question for you all, can you figure out what's wrong? 😉