C + + performance test

Posted by djmc48 on Sat, 20 Nov 2021 14:06:27 +0100

C + + performance test (I)

Write in front

Test environment:

Operating system: windows11
Compiler: msvc14

remarks:

It is recommended that those with c + + foundation come back to this blog, because I will not explain some functions or methods of c + + (because I may also check now). Of course, if there are any questions, I can answer them in the comment area. If not, I can search.
The test result is only obtained when I was learning c + + to test some functions or methods. The result is neither rigorous nor persuasive. My purpose is not to clarify the performance problems of c + +. I will not make any conclusions about the test results, but will briefly explain these results with what I know, so these test results are only for your reference, and you are welcome to criticize and correct.

Test method

int main()
{

	steady_clock::time_point t1;
	steady_clock::time_point t2;

	t1 = steady_clock::now();
	f1();
	t2 = steady_clock::now();
	std::cout << "f1() take " << duration_cast<microseconds>(t2 - t1).count() << " microsecond." << endl;


	return 0;
}

Test results (the time units of the following results are microseconds)

vector initialization

void f1() {
	vector<int> v(MAX_SIZE);
}

void f2() {
	vector<int> v(MAX_SIZE, 0);
}

void f3() {
	vector<int> v(MAX_SIZE, 99);
}

void f4() {
	vector<int> v(MAX_SIZE);
	for (int i = 0; i < MAX_SIZE; ++i) {
		v[i] = i;
	}
}

void f5() {
	vector<int> v;
	for (int i = 0; i < MAX_SIZE; ++i) {
		v.push_back(i);
	}
}

Output results

MAX_SIZEf1()f2()f3()f4()f5()
100000013651170287771249794325
10000000224171310827613860774957930
10000000014884913710929367376881729747950

f2() fast f1(), who knows? (funny)

Why is f4() faster than f5():

Because the size of a vector is dynamically allocated, there are two important attributes: size and capacity. If a vector of size is not initialized (such as the vector in f5()), its capacity is 0. The capacity will increase with the increase of the size of the vector, which means that if the capacity of the vector is 100 at the beginning and the size increases to 70, the capacity of the vector may be expanded to 200 (casually). When the vector finds enough memory, it will copy the data from the original memory to the new memory. Because f5() has no initialization capacity, it takes a lot of time to copy data.

loop

void f1(vector<int>& v) {
	int len = v.size();
	for (int i = 0; i < len; ++i) {
		v[i] = 1;
	}
}

void f2(vector<int>& v) {
	for (int i = 0; i < v.size(); ++i) {
		v[i] = 1;
	}
}

void f3(vector<int>& v) {
	for (int& i : v) {
		i = 1;
	}
}

void f4(vector<int>& v) {
	int i = v.size();
	while (i--) {
		v[i] = 1;
	}
}

void f5(vector<int>& v) {
	for_each(v.begin(), v.end(), [](int& i) {i = 1; });
}

inline void test(int& i) { i = 1; }
void f6(vector<int>& v) {
	for_each(v.begin(), v.end(), test);
}

Output results

MAX_SIZEf1()f2()f3()f4()f5()f6()
100000073341615925601027533824116
10000000541648594921133649884280339249
1000000006195801006894215256552774386767440570

Passing values, pointers, and references

void add_ref1(int& v,int &i) { v = i; }

int& add_ref2(int& v, int& i) { v = i; return v; }

void add_ptr(int* v, int& i) { *v = i; }

int add_val(int v, int& i) { v = i; return v; }

void f1(vector<int> v) {
	int j = 0;
	for (int& i : v) {
		add_ref1(i,++j);
	}
}

void f2(vector<int> v) {
	int j = 0;
	for (int& i : v) {
		i = add_ref2(i,++j);
	}
}

void f3(vector<int> v) {
	int j = 0;
	for (int& i : v) {
		add_ptr(&i,++j);
	}
}

void f4(vector<int> v) {
	int j = 0;
	for (int& i : v) {
		i = add_val(i,++j);
	}
}

Output results

MAX_SIZEf1()f2()f3()f4()
10000008725754089967263
1000000071787647097023365450
100000000647038682266687028614793

Sum

void f1(vector<int> v) {
	int sum = 0;
	for (int& i : v) {
		sum += i;
	}
	cout << sum << endl;
}

void f2(vector<int> v) {
	int sum = accumulate(v.begin(), v.end(), 0);
	cout << sum << endl;
}

void f3(int *v) {
	int sum = accumulate(v, v + MAX_SIZE, 0);
	cout << sum << endl;
}

Output results

MAX_SIZEf1()f2()f3()
100000629688501
1000000500810930-
100000004849669288-
100000000389700612720-

Note: because the size of the array is out of range, max_ There is no test data when the size is 1000000 and larger.

Topics: C++