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_SIZE | f1() | f2() | f3() | f4() | f5() |
---|---|---|---|---|---|
1000000 | 1365 | 1170 | 28777 | 12497 | 94325 |
10000000 | 22417 | 13108 | 276138 | 60774 | 957930 |
100000000 | 148849 | 137109 | 2936737 | 688172 | 9747950 |
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_SIZE | f1() | f2() | f3() | f4() | f5() | f6() |
---|---|---|---|---|---|---|
1000000 | 7334 | 16159 | 2560 | 10275 | 3382 | 4116 |
10000000 | 54164 | 85949 | 21133 | 64988 | 42803 | 39249 |
100000000 | 619580 | 1006894 | 215256 | 552774 | 386767 | 440570 |
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_SIZE | f1() | f2() | f3() | f4() |
---|---|---|---|---|
1000000 | 8725 | 7540 | 8996 | 7263 |
10000000 | 71787 | 64709 | 70233 | 65450 |
100000000 | 647038 | 682266 | 687028 | 614793 |
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_SIZE | f1() | f2() | f3() |
---|---|---|---|
100000 | 629 | 688 | 501 |
1000000 | 5008 | 10930 | - |
10000000 | 48496 | 69288 | - |
100000000 | 389700 | 612720 | - |
Note: because the size of the array is out of range, max_ There is no test data when the size is 1000000 and larger.