I have a structure in which there are three float variables, and in a bigger program, I'm using an array of these structure objects. Now, I thought that each time in a loop when I was using values of these structures, I was creating a new copy of those objects by assigning:
like
struct A copy1 = original;

So I thought, to improve performance, why not create a const reference of this variable, because I just want to use the values, not modify them.

But when I made a test program to compare their efficiences, I did not get much difference - and strangely, the const reference one gave lower performance.. Can you tell me why?
Here's the code i tested with:

// Hello2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<queue>
#include<stack>
#include<windows.h>
#include<winbase.h>
using namespace std;
#define K_LIMIT 10

struct A
{
	float c,d,e;
};


int main(int argc, char *argv[])
{
	LARGE_INTEGER start,end, start2, end2, start3, end3; 
	unsigned long i,j,k;
	LARGE_INTEGER startTime;   // For converting tick into real time
	LARGE_INTEGER finishTime;
	LARGE_INTEGER ticksPerSecond;

	// get the high resolution counter's accuracy
	QueryPerformanceFrequency(&ticksPerSecond);

	/*******/

	
	A something; 

	something.c=534.234;
	something.d=234.234;
	something.e=241.32;
	
	QueryPerformanceCounter(&start);
	for(i=0;i<10000;i++) for(j=0;j<10000;j++) for(k=0;k<K_LIMIT;k++) 
	{
		A other = something;
	}
	QueryPerformanceCounter(&end);

	//cout<<endl<<start<<endl<<end<<endl;

	cout<<"Time elapsed in first= "<<(end.QuadPart-start.QuadPart)/(float) ticksPerSecond.QuadPart<<endl;

	QueryPerformanceCounter(&start2);
	for(i=0;i<10000;i++) for(j=0;j<10000;j++) for(k=0;k<K_LIMIT;k++) 
	{
		const A& other2=something;
	}
	QueryPerformanceCounter(&end2);

	//cout<<endl<<start2<<endl<<end2<<endl; 	
	cout<<"Time elapsed in second= "<<(end2.QuadPart-start2.QuadPart)/(float) ticksPerSecond.QuadPart<<endl;

	
	getchar();
}

One more thing, noticed right now that these ticker measures are themselves skewed it seems.

When I just run the loop two times without any statements in them, they just give any damn value which may have a large difference upto around 25% !!!

Is my time measuring method wrong?

>>When I just run the loop two times without any statements in them, they just give any >>damn value which may have a large difference upto around 25% !!!

If you have optimization enabled and have an empty do-nothing loop then the compile will probably just delete the loop altogether. It will (or might) also delete the loop even it it is not empty but does nothing that changes, such as in the code you posted. For example the compiler will move line 42 up outside the loop and then delete the loop since it does nothing else.

Also lines 42 and 53 are doing something completly different. Line 53 just get the address of the structure (actually depending on implementation it may not even do that), while line 42 actually copies all the members of the structure and you wind up with two idential structure objects.

If you have optimization enabled and have an empty do-nothing loop then the compile will probably just delete the loop altogether. It will (or might) also delete the loop even it it is not empty but does nothing that changes, such as in the code you posted. For example the compiler will move line 42 up outside the loop and then delete the loop since it does nothing else.

I don't think then that my optimization is enabled. Because it is taking significant time to complete the nested loops I've given which is not the case when I remove the for loops.

Also lines 42 and 53 are doing something completly different. Line 53 just get the address of the structure (actually depending on implementation it may not even do that), while line 42 actually copies all the members of the structure and you wind up with two idential structure objects.

Yes, I'm aware of that and that was the main motive behind changing from the 1st approach to the 2nd because I want high efficiency in my main program. But in this test program itself, when it is giving random results, I can't implement that in that critical project :)

Any hints why it is doing that? I mean, at least copying a structure containing three float values should be costlier than just taking reference of that structure variable !

Thanks !!!

I didn't get your results. I compiled and ran your program using VC++ 2008 Express with debug on.

Time elapsed in first= 6.37265
Time elapsed in second= 3.19138

Humm... Seems my system is doing something weird. May be there's some other culprit process who's taking making my program take largely variable times. I'll look into it once again. Thanks anyway !!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.