Are pointers more efficient than using Variable?s

I have 2 pieces of code here, one that uses a pointer to change a variables value and another just using a variable.I get the same output on both, my question is which one is more efficient (faster build time,less memory)? Of course this program is small so it will be negligible but If i want to be a game developer in the future efficiency is a must.
Pointer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  using namespace std;
int triple(int *number);
int main()
{
	int num = 1;

	int *p = #

	cout << "The num is : " << num << endl;
	
	triple(p);

	cout << "The num is now: " << num << endl;
	
	return 0;
}

int triple(int *number)
{
	*number = *number * 3;
	
	return *number;
}

Using just a variable without a pointer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using namespace std;
int triple(int number);
int main()
{
	int num = 1;


	cout << "The num is : " << num << endl;
	
	num = triple(num);

	cout << "The num is now: " << num << endl;
	
	return 0;
}

int triple(int number)
{
	number = number * 3;
	
	return number;
}


Which method should I choose?
Last edited on
time it and see. Im sure results will vary between computers/compilers etc but something like this should show it up...

1
2
3
4
5
6
7
8
9
10
11
std::clock_t start,stop,delta1,delta2;

start=clock();
for (int i=0;i<10000;++i) (void)triple(num);
stop = clock();
delta1 = stop-start;

start=clock();
for (int i=0;i<10000;++i) (void)triple(p);
stop = clock();
delta2 = stop-start;
That depends. In your case passing by value will be always faster (rule of thumb: do not pass primitive types by pointer unless required)

In addition both these function does different things: first one changes its parameter. Which is not what you need if you want to triple a constant.

With complex types passing by pointer will be faster. However in C++ you should prefer passing by (const) reference instead. In fact, in C++ you should rarely see raw pointers at all.
Using variables without pointers is faster. It took 1 more second to use a pointer because I had to declare the pointer and reference num.
Last edited on
Did you time that jakee? If so what were the results? just curious.
As said above, using a pointer took 1 more second than not using a pointer. I thought pointers were more efficient :( So i assume you use pointers only when you have too..
pointer will be slower than primitive types. Because (1) primitive types are already as fast as it possible and (b) pointers add another level of indirection.

Pass by reference (do not use pointer aside from special cases) if faster when type is expensive to copy.
Pointers are just another tool to add to your kit, they are good for solving specific types of problems. In terms of game design, it's not something that you would want to run every time the screen updates, but they are useful in things like menu screens. It sounds like you are doing more beginning exercises regarding mastering the syntax of pointers.

They are used, in the real world, for more advanced concepts like linked lists and binary trees. Essentially, the best way to explain things like linked lists it is you create these little "shelves" that can store certain types of data. You use the pointers to "point" to these shelves, and iterate through them when. Essentially the concept of a linked list is like a row of shelves. You have a pointer that stays permanent, which is typically labelled something like "m_head", which is the first "shelf". You then make a temporary pointer that is equal to the first shelf. From there, you can check the first shelf, then go to the "m_next" (or next shelf) of "m_head" (the first shelf). So now the temporary pointer is pointing to the second shelf, you can check it, if it's not what you were looking for, you can iterate to the "m_next" (next shelf) of the second shelf (which is not the temp pointer).

So in short, yes it IS useful for games. You can create binary trees and linked lists for things like your inventory, search systems, etc. While you can use other methods (arrays, vectors, etc), things like binary search trees and linked lists give you much much more control for certain specific tasks, which in turn can allow you to make things more efficient. Also, using them can become very complicated, which will inevitably help you in the long run because you get used to solving more and more intricate and complex problems, making everything easier for you.
Topic archived. No new replies allowed.