Pass by value VS Pass by reference performance TEST

Hello.

Look at this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <conio.h>
#include <time.h>

using namespace std;

struct Cubo
{
	double a, b, c, d, e, f, g, h, i, l, m;
};

void f(Cubo c){}
void f2(Cubo &c){}

int main()
{
	int start{}, end{};
	Cubo mio{};

	start = time(0);

	for (int i{ 0 }; i < 300000000; i++)
	   f(mio);

	end = time(0);

	cout << "By value: " << difftime(end, start) << " seconds" << endl;

	start = time(0);

	for (int i{ 0 }; i < 300000000; i++)
	   f2(mio);

	end = time(0);

	cout << "By reference: " << difftime(end, start) << " seconds";

	_getch();
	return 0;
}


I tried this code on:

-Visual Studio in Debug mode
-Visual Studio in Release mode
-CodeBlocks with GCC

Well, the output is different for any of the ones mentioned above.

See this video: https://www.youtube.com/watch?v=G08RbsNJG5s&feature=youtu.be
You are not timing what you think you are timing.
In what sense?
> See this video
¡awesome, a video!
¿couldn't have simply pasted the times?
The cause is not the time(0) anyway.

It's the time it takes for the pass-by-reference which is MUCH SLOWER ON VISUAL STUDIO.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <conio.h>

using namespace std;

struct Cubo
{
     double arr[20]{};
};

void f(Cubo c) {}
void f2(Cubo& c) {}

int main()
{
	Cubo my;

	cout << "by value started" << endl;
	for (int i{ 0 }; i < 300000000; i++)
	     f(my);
	cout << "by value finished" << endl;


	cout << "by ref started" << endl;
	for (int i{ 0 }; i < 300000000; i++)
	     f2(my);
	cout << "by ref finished" << endl;


	_getch();
	return 0;
}


This doesn't use any reference to time functions like time(0).

On codeblocks the second for loop runs very faster than it does on visual studio.

Why?
Last edited on
The cause is not the time(0) anyway.

Nobody said it was.


This doesn't use any reference to time functions like time(0).

On codeblocks the second for loop runs very faster than it does on visual studio.

Why?

What makes you think it does? Your code can be completely optimized away by any optimizing compiler. Timing it is pointless.
Thanks for the answer.

Timing it is pointless.


In this case it is.
But If I'm making a huge program, much heavier than this, which uses a lot of memory and resources... well, what's the deal with this?

On one hand I get a very fast program, and on the other a rusted wagon?
Last edited on
But If I'm making a huge program, much heavier than this, which uses a lot of memory and resources... well, what's the deal with this?

Then I doubt your use cases will resemble anything at all like empty loops.

On one hand I get a very fast program, and on the other a rusted wagon?

Unless you're compiling with optimizations on, you can draw no conclusions from your observations, and as demonstrated with the links above, the timing is identical with similar optimization settings.

Perhaps you should spend some time getting familiar with your optimization settings.
You might not be timing anything at all. Since neither function does anything and the compiler can see that, it's entirely possible that the compiler optimized away the call altogether.

Try putting the definition of f() and f2() in a separate compilation unit to force the program to actually make the call.
Topic archived. No new replies allowed.