passing by value and by pointer using position independent code
I've been trying to figure out a few things to do with position indepentent code, specifically using gcc with -fpic.

I've written a function which mesures the time taken to pass a pointer across the stack a number of times, and then does the same by passing a copy. Mind you im my example the effect i see only works with struct or simple classes with no inheritance or virtual functions.

The thing im trying to get my head around is why when using PIC does the pointer become slower (by a little)? and why does the copy become much much faster as oposed to with no PIC. 

- im compiling with -03 also.


        class basicClass
        {
                private:
                char ar[2000];
        };

        void PassCopy(basicClass cpy)
        {
                static long count = 0;
                count++;
                if(count < 100000)
                PassCopy(cpy);
        }


        void PassPtr(basicClass * ptr)
        {
                static long count = 0;

                count++;
             
               if(count < 100000)
              
                 PassRegPtr(ptr);
        }


        void RunCopyTest()

        {
        
                   basicClass c;

                  timeval tv1, tv2;

                  gettimeofday(&tv1, NULL);
                  PassCopy(c);
                  gettimeofday(&tv2, NULL);

                  long long diff1 = (tv2.tv_sec * MICROSECONDS_PER_SEC + tv2.tv_usec)
                                                        - (tv1.tv_sec * MICROSECONDS_PER_SEC + tv1.tv_usec);

         
                  basicClass *pc= new basicClass();
                  gettimeofday(&tv1, NULL);
                  PassRegPtr(pc);
                  gettimeofday(&tv2, NULL);
                  delete pc;

                  long long diff2 = (tv2.tv_sec * MICROSECONDS_PER_SEC + tv2.tv_usec)
                                                                - (tv1.tv_sec * MICROSECONDS_PER_SEC + tv1.tv_usec);

                  std::cout << "Diff 1 = " << diff1 << " Diff 2 = " << diff2 << std::endl;

          }
passing by value and by pointer using position independent code
===

I've been trying to figure out a few things to do with position indepentent code, specifically using gcc with -fpic.

I've written a function which mesures the time taken to pass a pointer across the stack a number of times, and then does the same by passing a copy. Mind you im my example the effect i see only works with struct or simple classes with no inheritance or virtual functions.

The thing im trying to get my head around is why when using PIC does the pointer become slower (by a little)? and why does the copy become much much faster as oposed to with no PIC. 

- im compiling with -03 also.


        class basicClass
        {
                private:
                char ar[2000];
        };

        void PassCopy(basicClass cpy)
        {
                static long count = 0;
                count++;
                if(count < 100000)
                PassCopy(cpy);
        }


        void PassPtr(basicClass * ptr)
        {
                static long count = 0;

                count++;
             
               if(count < 100000)
              
                 PassRegPtr(ptr);
        }


        void RunCopyTest()

        {
        
                   basicClass c;

                  timeval tv1, tv2;

                  gettimeofday(&tv1, NULL);
                  PassCopy(c);
                  gettimeofday(&tv2, NULL);

                  long long diff1 = (tv2.tv_sec * MICROSECONDS_PER_SEC + tv2.tv_usec)
                                                        - (tv1.tv_sec * MICROSECONDS_PER_SEC + tv1.tv_usec);

         
                  basicClass *pc= new basicClass();
                  gettimeofday(&tv1, NULL);
                  PassRegPtr(pc);
                  gettimeofday(&tv2, NULL);
                  delete pc;

                  long long diff2 = (tv2.tv_sec * MICROSECONDS_PER_SEC + tv2.tv_usec)
                                                                - (tv1.tv_sec * MICROSECONDS_PER_SEC + tv1.tv_usec);

                  std::cout << "Diff 1 = " << diff1 << " Diff 2 = " << diff2 << std::endl;

          }