which is most efficient when using std::vector?

Hello, both codes below do exactly the same and both are working. Here is my question for those more experienced. Which of the two codes below would be the most efficient when working with large vectors?

Code 1:
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
#include <iostream>
#include <vector>
using namespace std;

void foo(vector<double>& temp, int length);

int main()
{
	int size = 10000;
	int i;

	vector<double> vec;
	vec.reserve(size);
	
	foo(vec, size);

	for (i = 0; i < size; i++)
		cout << "vec[" << i << "] =" << vec[i] << endl;
	cout << endl;
	
	return 0;
}

void foo(vector<double>& temp, int length)
{
	int i;
	
	for (i = 0; i < length; i++)
		temp.push_back((i + 1) * (i + 1));
}


code 2:
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
#include <iostream>
#include <vector>
using namespace std;

vector<double> foo(int length);

int main()
{
	int size = 10000;
	int i;
	
	vector<double> vec = foo(size);
	
	for (i = 0; i < size; i++)
		cout << "vec[" << i << "] =" << vec[i] << endl;
	cout << endl;
	
	return 0;
}

vector<double> foo(int length)
{
	int i;
	
	vector<double> temp;
	temp.reserve(length);
	
	for (i = 0; i < length; i++)
		temp.push_back((i + 1) * (i + 1));
	
	return temp;
}


Is there any other way to do the same that is yet more efficient?

Thanks.
Last edited on
Version 1 is exactly n times faster than version 2.
This is because you're returning a vector. This means that the vector needs to copied when returned. I don't need to say what this would entail for long vectors or big elements. It could take a while.

Version 1 is more or less as efficient as it gets.
Last edited on
Thanks again. And how about this pointer version?:

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 <vector>
using namespace std;

typedef vector<double> VecType;
	
VecType* foo(int length);

int main()
{
	int size = 10000;
	int i;
	
	VecType* vec = foo(size);
	
	for (i = 0; i < size; i++)
		cout << "vec[" << i << "] =" << (*vec)[i] << endl;
	cout << endl;
	
	return 0;
}

VecType* foo(int length)
{
	int i;
	
	VecType* temp = new VecType(length);
	
	for (i = 0; i < length; i++)
		(*temp)[i] = (i + 1) * (i + 1);
	
	return temp;
}


Is it yet faster than the two previous versions?
Nope. Just as fast as version 1 in the other post.
Notice, however, that you're not deleting vec after you're done with it.
This is not a problem in this case, since the program terminates immediately, but if that was a function that is called several times you would create quite a few memory leaks.

PS: I realized soon after that my calculation in the above post was wrong, but I was too lazy to change it. Version 2 takes a little less than twice as long as version 1.
Last edited on
Ok. As always, thanks for your valuable reply. My concern is I am working with large scale problems, so my vectors are large. With this in mind, do you know if there is a more efficient way to do the operations above? or version 1 or the pointer version are the fastest way I can work?
I like version 1 in your first post the best.
Topic archived. No new replies allowed.