📜 ⬆️ ⬇️

Numeric types and C ++ containers in terms of performance

Often we all have to deal with processing large amounts of data. This note is a discussion on which containers and numeric types (including various operations) best cope with this task. In the books, there are sometimes references to how to increase productivity with the help of a choice of one type or another, but no specific figures are given.

Notes


So, the list of processors that were used to measure


For the source code of the program here
Hidden text
#include <iostream> #include <cstdint> #include <vector> #include <cmath> #include <fstream> #include <ctime> template <typename T> void testValue(T val); template <typename T1, typename T2> void testAssignment(T1 container, T2 val); template <typename T1, typename T2> void testAssignmentCArr(T1 container, T2 val); template <typename T1, typename T2> void testSubtraction(T1 container, T2 val); template <typename T1, typename T2> void testSubtractionCArr(T1 container, T2 val); template <typename T1, typename T2> void testDivision(T1 container, T2 val); template <typename T1, typename T2> void testDivisionCArr(T1 container, T2 val); template <typename T1, typename T2> void testPush(T1 container, T2 val); template <typename T1, typename T2> void testPushCArr(T1 *container, T2 val); using namespace std; const uint16_t ARR_SIZE = 512; ofstream outFile; double cl; vector<unsigned char> vec1(ARR_SIZE); vector<unsigned short> vec2(ARR_SIZE); vector<unsigned int> vec3(ARR_SIZE); vector<unsigned long> vec4(ARR_SIZE); vector<unsigned long long> vec5(ARR_SIZE); vector<uint8_t> vec6(ARR_SIZE); vector<uint16_t> vec7(ARR_SIZE); vector<uint32_t> vec8(ARR_SIZE); vector<uint64_t> vec9(ARR_SIZE); vector<float> vec10(ARR_SIZE); vector<double> vec11(ARR_SIZE); vector<long double> vec12(ARR_SIZE); unsigned char *cArr1 = new unsigned char [ARR_SIZE]; unsigned short *cArr2 = new unsigned short [ARR_SIZE]; unsigned int *cArr3 = new unsigned int [ARR_SIZE]; unsigned long *cArr4 = new unsigned long [ARR_SIZE]; unsigned long long *cArr5 = new unsigned long long [ARR_SIZE]; uint8_t *cArr6 = new uint8_t [ARR_SIZE]; uint16_t *cArr7 = new uint16_t [ARR_SIZE]; uint32_t *cArr8 = new uint32_t [ARR_SIZE]; uint64_t *cArr9 = new uint64_t [ARR_SIZE]; float *cArr10 = new float [ARR_SIZE]; double *cArr11 = new double [ARR_SIZE]; long double *cArr12 = new long double [ARR_SIZE]; int main(int argc, char *argv[]) { outFile.open("result"); testValue((uint8_t) 0xff); testValue((uint16_t) 0xffff); testValue((uint32_t) 0xffffffff); testValue((uint64_t) 0xffffffffffffffff); outFile.close(); } template <typename T> void testValue(T val) { outFile << hex; outFile << endl << " VALUE: 0x" << uint64_t(val) << endl; outFile << dec; outFile << endl << "  ."; outFile << endl << " VEC: "; testAssignment(vec1, val); testAssignment(vec2, val); testAssignment(vec3, val); testAssignment(vec4, val); testAssignment(vec5, val); testAssignment(vec6, val); testAssignment(vec7, val); testAssignment(vec8, val); testAssignment(vec9, val); testAssignment(vec10, val); testAssignment(vec11, val); testAssignment(vec12, val); outFile << endl << " CARR: "; testAssignmentCArr(cArr1, val); testAssignmentCArr(cArr2, val); testAssignmentCArr(cArr3, val); testAssignmentCArr(cArr4, val); testAssignmentCArr(cArr5, val); testAssignmentCArr(cArr6, val); testAssignmentCArr(cArr7, val); testAssignmentCArr(cArr8, val); testAssignmentCArr(cArr9, val); testAssignmentCArr(cArr10, val); testAssignmentCArr(cArr11, val); testAssignmentCArr(cArr12, val); outFile << endl << "  ."; outFile << endl << " VEC: "; testSubtraction(vec1, 8); testSubtraction(vec2, 8); testSubtraction(vec3, 8); testSubtraction(vec4, 8); testSubtraction(vec5, 8); testSubtraction(vec6, 8); testSubtraction(vec7, 8); testSubtraction(vec8, 8); testSubtraction(vec9, 8); testSubtraction(vec10, 8); testSubtraction(vec11, 8); testSubtraction(vec12, 8); outFile << endl << " CARR: "; testSubtractionCArr(cArr1, 8); testSubtractionCArr(cArr2, 8); testSubtractionCArr(cArr3, 8); testSubtractionCArr(cArr4, 8); testSubtractionCArr(cArr5, 8); testSubtractionCArr(cArr6, 8); testSubtractionCArr(cArr7, 8); testSubtractionCArr(cArr8, 8); testSubtractionCArr(cArr9, 8); testSubtractionCArr(cArr10, 8); testSubtractionCArr(cArr11, 8); testSubtractionCArr(cArr12, 8); outFile << endl << "  ."; outFile << endl << " VEC: "; testDivision(vec1, 2); testDivision(vec2, 2); testDivision(vec3, 2); testDivision(vec4, 2); testDivision(vec5, 2); testDivision(vec6, 2); testDivision(vec7, 2); testDivision(vec8, 2); testDivision(vec9, 2); testDivision(vec10, 2); testDivision(vec11, 2); testDivision(vec12, 2); outFile << endl << " CARR: "; testDivisionCArr(cArr1, 2); testDivisionCArr(cArr2, 2); testDivisionCArr(cArr3, 2); testDivisionCArr(cArr4, 2); testDivisionCArr(cArr5, 2); testDivisionCArr(cArr6, 2); testDivisionCArr(cArr7, 2); testDivisionCArr(cArr8, 2); testDivisionCArr(cArr9, 2); testDivisionCArr(cArr10, 2); testDivisionCArr(cArr11, 2); testDivisionCArr(cArr12, 2); outFile << endl << "     ."; outFile << endl << " VEC: "; testPush(vec1, 4); testPush(vec2, 4); testPush(vec3, 4); testPush(vec4, 4); testPush(vec5, 4); testPush(vec6, 4); testPush(vec7, 4); testPush(vec8, 4); testPush(vec9, 4); testPush(vec10, 4); testPush(vec11, 4); testPush(vec12, 4); outFile << endl << " CARR: "; testPushCArr(cArr1, 4); testPushCArr(cArr2, 4); testPushCArr(cArr3, 4); testPushCArr(cArr4, 4); testPushCArr(cArr5, 4); testPushCArr(cArr6, 4); testPushCArr(cArr7, 4); testPushCArr(cArr8, 4); testPushCArr(cArr9, 4); testPushCArr(cArr10, 4); testPushCArr(cArr11, 4); testPushCArr(cArr12, 4); outFile << endl; } template <typename T1, typename T2> void testAssignment(T1 container, T2 val) { cl = clock(); for (auto &k : container) for (auto &j : container) for (auto &i : container) { i = val; j = val; k = val; } outFile << (clock() - cl) / CLOCKS_PER_SEC << " "; } template <typename T1, typename T2> void testAssignmentCArr(T1 container, T2 val) { cl = clock(); for (uint16_t k = 0; k < ARR_SIZE; k++) for (uint16_t j = 0; j < ARR_SIZE; j++) for (uint16_t i = 0; i < ARR_SIZE; i++) { container[i] = val; container[j] = val; container[k] = val; } outFile << (clock() - cl) / CLOCKS_PER_SEC << " "; } template <typename T1, typename T2> void testSubtraction(T1 container, T2 val) { cl = clock(); for (auto &k : container) for (auto &j : container) for (auto &i : container) { i -= val; j -= val; k -= val; } outFile << (clock() - cl) / CLOCKS_PER_SEC << " "; } template <typename T1, typename T2> void testSubtractionCArr(T1 container, T2 val) { cl = clock(); for (uint16_t k = 0; k < ARR_SIZE; k++) for (uint16_t j = 0; j < ARR_SIZE; j++) for (uint16_t i = 0; i < ARR_SIZE; i++) { container[i] -= val; container[j] -= val; container[k] -= val; } outFile << (clock() - cl) / CLOCKS_PER_SEC << " "; } template <typename T1, typename T2> void testDivision(T1 container, T2 val) { cl = clock(); for (auto &k : container) for (auto &j : container) for (auto &i : container) { i /= val; j /= val; k /= val; } outFile << (clock() - cl) / CLOCKS_PER_SEC << " "; } template <typename T1, typename T2> void testDivisionCArr(T1 container, T2 val) { cl = clock(); for (uint16_t k = 0; k < ARR_SIZE; k++) for (uint16_t j = 0; j < ARR_SIZE; j++) for (uint16_t i = 0; i < ARR_SIZE; i++) { container[i] /= val; container[j] /= val; container[k] /= val; } outFile << (clock() - cl) / CLOCKS_PER_SEC << " "; } template <typename T1, typename T2> void testPush(T1 container, T2 val) { cl = clock(); for (uint16_t j = 0; j < ARR_SIZE; j++) for (uint16_t i = 0; i < ARR_SIZE; i++) { for (int l = 0; l < val; l++) container.push_back(0); } outFile << (clock() - cl) / CLOCKS_PER_SEC << " "; } template <typename T1, typename T2> void testPushCArr(T1 *container, T2 val) { cl = clock(); for (uint16_t j = 0; j < ARR_SIZE; j++) for (uint16_t i = 0; i < ARR_SIZE; i++) { T1 *tmp = new T1 [ARR_SIZE + val]; for (uint16_t l = 0; l < ARR_SIZE + val; l++) { if (l < ARR_SIZE) tmp[l] = container[l]; else tmp[l] = 0; } container = tmp; delete [] tmp; } outFile << (clock() - cl) / CLOCKS_PER_SEC << " "; } 


The result of the execution is a file with four tables. For more accurate figures, measurements were carried out seven times. At the end, the results were averaged.
')
What did you find out?

AMD Athlon 64 X2 4800+



For a list ordered by time here
Hidden text
Assignment operation (std :: vector)

1. float (0.17 sec.)
2. double (0.17 sec.)
3. long double (0.17 sec.)
4. unsigned short (0.22 sec.)
5. uint16_t (0.22 sec.)
6. unsigned long (0.24 sec.)
7. uint32_t (0.24 sec.)
8. unsigned int (0.25 sec.)
9. unsigned char (0.36 sec.)
10. uint8_t (0.36 sec.)
11. uint64_t (0.4 sec.)
12. unsigned long long (0.41 sec.)

Assignment operation (c-style array)

1. unsigned short (0.16 sec.)
2. unsigned int (0.16 sec.)
3. unsigned long (0.16 sec.)
4. uint32_t (0.16 sec.)
5. double (0.16 sec.)
6. long double (0.16 sec.)
7. unsigned char (0.17 sec.)
8. uint16_t (0.17 sec.)
9. float (0.17 sec.)
10. uint8_t (0.18 sec.)
11. unsigned long long (0.22 sec.)
12. uint64_t (0.22 sec.)

Subtract operation (std :: vector)

1. unsigned short (0.5 sec.)
2. uint16_t (0.51 sec.)
3. unsigned int (0.52 sec.)
4. unsigned long (0.52 sec.)
5. uint32_t (0.52 sec.)
6. unsigned char (0.58 sec.)
7. uint8_t (0.58 sec.)
8. double (0.72 sec.)
9. long double (0.72 sec.)
10. float (0.74 sec.)
11. unsigned long long (1.22 sec.)
12. uint64_t (1.22 sec.)

Subtraction operation (c-style array)

1. unsigned char (0.42 sec.)
2. uint8_t (0.42 sec.)
3. uint16_t (0.42 sec.)
4. unsigned short (0.44 sec.)
5. unsigned int (0.51 sec.)
6. unsigned long (0.51 sec.)
7. uint32_t (0.51 sec.)
8. double (0.7 sec.)
9. float (0.71 sec.)
10. long double (0.71 sec.)
11. unsigned long long (1.02 sec.)
12. uint64_t (1.02 sec.)

Division operation (std :: vector)

1. float (1.4 sec.)
2. long double (1.4 sec.)
3. double (1.44 sec.)
4. unsigned int (6.65 sec.)
5. unsigned long (6.66 sec.)
6. uint32_t (6.78 sec.)
7. unsigned char (6.97 sec.)
8. uint8_t (6.97 sec.)
9. unsigned short (7 sec.)
10. uint16_t (7.05 sec.)
11. uint64_t (16.07 seconds)
12. unsigned long long (16.08 sec.)

Division operation (c-style array)

1. unsigned char (0.42 sec.)
2. uint8_t (0.42 sec.)
3. unsigned short (0.47 sec.)
4. uint16_t (0.47 sec.)
5. unsigned int (0.5 sec.)
6. uint32_t (0.5 sec.)
7. unsigned long (0.51 sec.)
8. uint64_t (0.71 sec.)
9. float (0.71 sec.)
10. double (0.71 sec.)
11. long double (0.71 sec.)
12. unsigned long long (0.73 sec.)

Adding elements to the end of the array (std :: vector)

1. unsigned char (0.01 sec.)
2. unsigned short (0.01 sec.)
3. uint8_t (0.01 sec.)
4. uint16_t (0.01 sec.)
5. unsigned int (0.02 sec.)
6. unsigned long (0.02 sec.)
7. uint32_t (0.02 sec.)
8. float (0.02 sec.)
9. unsigned long long (0.03 sec.)
10. uint64_t (0.03 seconds)
11. double (0.03 sec.)
12. long double (0.03 seconds)

Adding elements to the end of the array (c-style array)

1. unsigned char (0.36 sec.)
2. unsigned int (0.36 sec.)
3. unsigned long (0.36 sec.)
4. unsigned long long (0.36 sec.)
5. uint8_t (0.36 sec.)
6. uint32_t (0.36 sec.)
7. uint64_t (0.36 sec.)
8. float (0.36 sec.)
9. unsigned short (0.42 sec.)
10. uint16_t (0.42 sec.)
11. double (0.42 sec.)
12. long double (0.42 sec.)

AMD E2-3000M



For a list ordered by time here
Hidden text
Assignment operation (std :: vector)

1. unsigned char (0.14 sec.)
2. uint8_t (0.14 sec.)
3. unsigned int (0.15 sec.)
4. uint32_t (0.15 sec.)
5. unsigned short (0.16 sec.)
6. unsigned long (0.16 sec.)
7. uint16_t (0.16 sec.)
8. double (0.22 sec.)
9. float (0.23 sec.)
10. unsigned long long (0.24 sec.)
11. uint64_t (0.24 sec.)
12. long double (1.47 sec.)

Assignment operation (c-style array)

1. unsigned char (0.18 sec.)
2. unsigned int (0.18 sec.)
3. unsigned long (0.18 sec.)
4. uint8_t (0.18 sec.)
5. uint32_t (0.18 sec.)
6. unsigned short (0.19 sec.)
7. uint16_t (0.19 sec.)
8. float (0.22 sec.)
9. double (0.22 sec.)
10. unsigned long long (0.24 sec.)
11. uint64_t (0.24 sec.)
12. long double (1.47 sec.)

Subtract operation (std :: vector)

1. uint32_t (0.51 sec.)
2. unsigned int (0.52 sec.)
3. unsigned long (0.52 sec.)
4. unsigned char (0.55 sec.)
5. uint8_t (0.55 sec.)
6. uint16_t (0.55 sec.)
7. unsigned short (0.56 sec.)
8. unsigned long long (0.7 sec.)
9. uint64_t (0.7 sec.)
10. float (0.93 sec.)
11. double (0.93 sec.)
12. long double (2.18 sec.)

Subtraction operation (c-style array)

1. unsigned int (0.51 sec.)
2. unsigned long (0.51 sec.)
3. uint32_t (0.51 sec.)
4. unsigned char (0.55 sec.)
5. unsigned short (0.55 sec.)
6. uint8_t (0.55 sec.)
7. uint16_t (0.55 sec.)
8. unsigned long long (0.69 sec.)
9. uint64_t (0.69 sec.)
10. float (0.92 sec.)
11. double (0.93 sec.)
12. long double (2.17 sec.)

Division operation (std :: vector)

1. unsigned int (1.96 sec.)
2. unsigned long (1.96 sec.)
3. uint32_t (1.96 sec.)
4. unsigned char (2.14 sec.)
5. unsigned short (2.14 sec.)
6. uint8_t (2.14 seconds)
7. uint16_t (2.14 seconds)
8. unsigned long long (3.65 sec.)
9. uint64_t (3.65 sec.)
10. float (4.22 sec.)
11. double (4.22 sec.)
12. long double (5.27 sec.)

Division operation (c-style array)

1. unsigned int (1.98 sec.)
2. unsigned long (1.98 sec.)
3. uint32_t (1.98 sec.)
4. unsigned char (2.16 sec.)
5. uint16_t (2.16 sec.)
6. unsigned short (2.17 sec.)
7. uint8_t (2.17 seconds)
8. unsigned long long (3.96 sec.)
9. uint64_t (3.96 sec.)
10. float (4.22 sec.)
11. double (4.23 sec.)
12. long double (5.29 seconds)

Adding elements to the end of the array (std :: vector)

1. unsigned char (0.01 sec.)
2. unsigned short (0.01 sec.)
3. uint8_t (0.01 sec.)
4. uint16_t (0.01 sec.)
5. unsigned int (0.02 sec.)
6. unsigned long (0.02 sec.)
7. uint32_t (0.02 sec.)
8. float (0.02 sec.)
9. unsigned long long (0.03 sec.)
10. uint64_t (0.03 seconds)
11. double (0.03 sec.)
12. long double (0.04 sec.)

Adding elements to the end of the array (c-style array)

1. unsigned int (0.24 sec.)
2. unsigned long (0.24 sec.)
3. uint32_t (0.24 sec.)
4. double (0.24 sec.)
5. unsigned long long (0.26 sec.)
6. uint64_t (0.27 sec.)
7. unsigned short (0.31 sec.)
8. uint16_t (0.31 sec.)
9. float (0.31 sec.)
10. unsigned char (0.72 sec.)
11. uint8_t (0.72 sec.)
12. long double (0.73 sec.)

Intel Core i5-3230M



For a list ordered by time here
Hidden text
Assignment operation (std :: vector)

1. float (0.12 sec.)
2. double (0.12 sec.)
3. long double (0.12 sec.)
4. uint16_t (0.14 sec.)
5. unsigned short (0.15 sec.)
6. unsigned int (0.17 sec.)
7. unsigned long (0.17 sec.)
8. uint32_t (0.17 sec.)
9. unsigned char (0.23 sec.)
10. uint8_t (0.23 sec.)
11. unsigned long long (0.28 sec.)
12. uint64_t (0.28 sec.)

Assignment operation (c-style array)

1. unsigned char (0.11 sec.)
2. unsigned short (0.11 sec.)
3. unsigned int (0.11 sec.)
4. unsigned long (0.11 sec.)
5. uint8_t (0.11 sec.)
6. uint32_t (0.11 sec.)
7. float (0.11 sec.)
8. double (0.11 sec.)
9. long double (0.11 sec.)
10. uint16_t (0.12 sec.)
11. unsigned long long (0.22 sec.)
12. uint64_t (0.22 sec.)

Subtract operation (std :: vector)

1. unsigned short (0.23 sec.)
2. unsigned int (0.23 sec.)
3. unsigned long (0.23 sec.)
4. uint16_t (0.23 sec.)
5. uint32_t (0.23 sec.)
6. unsigned char (0.27 sec.)
7. uint8_t (0.27 sec.)
8. float (0.33 sec.)
9. double (0.33 sec.)
10. long double (0.33 sec.)
11. uint64_t (0.41 seconds)
12. unsigned long long (0.42 sec.)

Subtraction operation (c-style array)

1. unsigned char (0.23 sec.)
2. unsigned short (0.23 sec.)
3. unsigned int (0.23 sec.)
4. unsigned long (0.23 sec.)
5. uint8_t (0.23 sec.)
6. uint16_t (0.23 sec.)
7. uint32_t (0.23 sec.)
8. unsigned long long (0.31 sec.)
9. uint64_t (0.31 seconds)
10. float (0.33 sec.)
11. double (0.33 sec.)
12. long double (0.33 sec.)

Division operation (std :: vector)

1. float (0.77 sec.)
2. double (0.89 sec.)
3. long double (0.89 sec.)
4. unsigned char (1 sec.)
5. unsigned short (1 sec.)
6. uint8_t (1 sec.)
7. uint16_t (1 sec.)
8. unsigned int (1.14 sec.)
9. uint32_t (1.14 sec.)
10. unsigned long (1.15 sec.)
11. unsigned long long (3.2 sec.)
12. uint64_t (3.21 seconds)

Division operation (c-style array)

1. unsigned char (0.23 sec.)
2. unsigned short (0.23 sec.)
3. unsigned int (0.23 sec.)
4. unsigned long (0.23 sec.)
5. uint8_t (0.23 sec.)
6. uint16_t (0.23 sec.)
7. uint32_t (0.23 sec.)
8. unsigned long long (0.26 sec.)
9. uint64_t (0.26 sec.)
10. float (0.41 sec.)
11. double (0.41 seconds)
12. long double (0.41 seconds)

Adding elements to the end of the array (std :: vector)

1. unsigned char (0 sec.)
2. unsigned short (0 sec.)
3. uint8_t (0 sec.)
4. uint16_t (0 sec.)
5. unsigned int (0.01 sec.)
6. unsigned long (0.01 sec.)
7. unsigned long long (0.01 sec.)
8. uint32_t (0.01 sec.)
9. uint64_t (0.01 sec.)
10. float (0.01 sec.)
11. double (0.01 sec.)
12. long double (0.01 sec.)

Adding elements to the end of the array (c-style array)

1. unsigned char (0.13 sec.)
2. unsigned short (0.13 sec.)
3. unsigned int (0.13 sec.)
4. unsigned long long (0.13 sec.)
5. uint8_t (0.13 sec.)
6. uint16_t (0.13 sec.)
7. uint32_t (0.13 sec.)
8. uint64_t (0.13 sec.)
9. float (0.13 sec.)
10. double (0.13 sec.)
11. long double (0.13 sec.)
12. unsigned long (0.16 sec.)

Intel Pentium CPU B960



For a list ordered by time here
Hidden text
Assignment operation (std :: vector)

1. float (0.19 sec.)
2. double (0.19 sec.)
3. long double (0.19 sec.)
4. unsigned short (0.23 sec.)
5. uint16_t (0.23 sec.)
6. unsigned int (0.28 sec.)
7. unsigned long (0.28 sec.)
8. uint32_t (0.28 sec.)
9. unsigned char (0.4 sec.)
10. uint8_t (0.4 sec.)
11. unsigned long long (0.45 sec.)
12. uint64_t (0.45 sec.)

Assignment operation (c-style array)

1. unsigned short (0.18 sec.)
2. unsigned long (0.18 sec.)
3. double (0.18 sec.)
4. unsigned char (0.19 sec.)
5. unsigned int (0.19 sec.)
6. uint8_t (0.19 sec.)
7. uint16_t (0.19 sec.)
8. uint32_t (0.19 sec.)
9. float (0.19 sec.)
10. long double (0.19 sec.)
11. unsigned long long (0.37 sec.)
12. uint64_t (0.37 sec.)

Subtract operation (std :: vector)

1. unsigned short (0.41 sec.)
2. unsigned long (0.41 sec.)
3. uint8_t (0.41 sec.)
4. uint32_t (0.41 seconds)
5. unsigned char (0.42 sec.)
6. unsigned int (0.42 sec.)
7. uint16_t (0.42 sec.)
8. uint64_t (0.53 sec.)
9. unsigned long long (0.54 sec.)
10. float (0.55 sec.)
11. double (0.55 sec.)
12. long double (0.55 sec.)

Subtraction operation (c-style array)

1. unsigned short (0.41 sec.)
2. uint16_t (0.41 sec.)
3. unsigned char (0.42 sec.)
4. unsigned int (0.42 sec.)
5. unsigned long (0.42 sec.)
6. uint8_t (0.42 sec.)
7. uint32_t (0.42 sec.)
8. unsigned long long (0.51 sec.)
9. uint64_t (0.51 sec.)
10. float (0.55 sec.)
11. double (0.55 sec.)
12. long double (0.55 sec.)

Division operation (std :: vector)

1. float (1.84 sec.)
2. double (1.84 sec.)
3. long double (1.84 sec.)
4. unsigned short (1.97 sec.)
5. uint16_t (1.97 sec.)
6. unsigned char (1.98 sec.)
7. unsigned int (1.98 sec.)
8. unsigned long (1.98 sec.)
9. uint8_t (1.98 sec.)
10. uint32_t (1.98 sec.)
11. unsigned long long (4.96 sec.)
12. uint64_t (4.96 seconds)

Division operation (c-style array)

1. unsigned char (0.44 sec.)
2. unsigned long long (0.44 sec.)
3. uint8_t (0.44 sec.)
4. uint64_t (0.44 sec.)
5. unsigned short (0.45 sec.)
6. unsigned int (0.45 sec.)
7. unsigned long (0.45 sec.)
8. uint16_t (0.45 sec.)
9. uint32_t (0.45 sec.)
10. float (0.68 sec.)
11. double (0.68 sec.)
12. long double (0.68 sec.)

Adding elements to the end of the array (std :: vector)

1. unsigned char (0 sec.)
2. unsigned short (0.01 sec.)
3. unsigned int (0.01 sec.)
4. unsigned long (0.01 sec.)
5. uint8_t (0.01 sec.)
6. uint16_t (0.01 sec.)
7. uint32_t (0.01 sec.)
8. float (0.01 sec.)
9. unsigned long long (0.02 sec.)
10. uint64_t (0.02 sec.)
11. double (0.02 sec.)
12. long double (0.02 sec.)

Adding elements to the end of the array (c-style array)

1. unsigned char (0.21 sec.)
2. unsigned short (0.21 sec.)
3. unsigned int (0.21 sec.)
4. uint8_t (0.21 sec.)
5. uint16_t (0.21 sec.)
6. uint32_t (0.21 sec.)
7. uint64_t (0.21 sec.)
8. double (0.21 sec.)
9. long double (0.21 sec.)
10. unsigned long long (0.22 sec.)
11. float (0.22 sec.)
12. unsigned long (0.23 sec.)

Intel Pentium CPU G850



For a list ordered by time here
Hidden text
Assignment operation (std :: vector)

1. unsigned short (0.14 sec.)
2. unsigned int (0.14 sec.)
3. unsigned long (0.14 sec.)
4. unsigned long long (0.14 sec.)
5. uint8_t (0.14 sec.)
6. uint16_t (0.14 sec.)
7. uint32_t (0.14 sec.)
8. uint64_t (0.14 sec.)
9. float (0.14 sec.)
10. double (0.14 sec.)
11. unsigned char (0.15 sec.)
12. long double (0.81 sec.)

Assignment operation (c-style array)

1. float (0.14 sec.)
2. double (0.14 sec.)
3. unsigned short (0.18 sec.)
4. unsigned long (0.18 sec.)
5. unsigned long long (0.18 sec.)
6. uint16_t (0.18 sec.)
7. uint64_t (0.18 sec.)
8. unsigned char (0.19 sec.)
9. unsigned int (0.19 sec.)
10. uint8_t (0.19 sec.)
11. uint32_t (0.19 sec.)
12. long double (0.79 sec.)

Subtract operation (std :: vector)

1. unsigned char (0.31 sec.)
2. unsigned short (0.31 sec.)
3. unsigned int (0.31 sec.)
4. unsigned long (0.31 sec.)
5. unsigned long long (0.31 sec.)
6. uint8_t (0.31 sec.)
7. uint16_t (0.31 seconds)
8. uint32_t (0.31 sec.)
9. uint64_t (0.31 seconds)
10. float (0.42 sec.)
11. double (0.42 sec.)
12. long double (0.97 sec.)

Subtraction operation (c-style array)

1. unsigned char (0.33 sec.)
2. unsigned short (0.33 sec.)
3. unsigned int (0.33 sec.)
4. unsigned long (0.33 sec.)
5. unsigned long long (0.33 sec.)
6. uint8_t (0.33 sec.)
7. uint16_t (0.33 sec.)
8. uint32_t (0.33 sec.)
9. uint64_t (0.33 sec.)
10. float (0.42 sec.)
11. double (0.42 sec.)
12. long double (0.98 sec.)

Division operation (std :: vector)

1. float (1.4 sec.)
2. double (1.4 sec.)
3. long double (1.4 sec.)
4. unsigned int (1.49 sec.)
5. uint32_t (1.49 sec.)
6. unsigned short (1.5 sec.)
7. uint16_t (1.5 sec.)
8. unsigned char (1.51 sec.)
9. uint8_t (1.51 sec.)
10. unsigned long (3.33 sec.)
11. unsigned long long (3.33 sec.)
12. uint64_t (3.33 sec.)

Division operation (c-style array)

1. float (1.4 sec.)
2. double (1.4 sec.)
3. long double (1.41 sec.)
4. unsigned int (1.49 sec.)
5. uint32_t (1.49 sec.)
6. unsigned char (1.51 sec.)
7. unsigned short (1.51 sec.)
8. uint8_t (1.51 sec.)
9. uint16_t (1.51 sec.)
10. unsigned long long (3.24 sec.)
11. uint64_t (3.24 sec.)
12. unsigned long (3.25 sec.)

Adding elements to the end of the array (std :: vector)

1. unsigned char (0 sec.)
2. unsigned short (0 sec.)
3. uint8_t (0 sec.)
4. uint16_t (0 sec.)
5. unsigned int (0.01 sec.)
6. unsigned long (0.01 sec.)
7. unsigned long long (0.01 sec.)
8. uint32_t (0.01 sec.)
9. uint64_t (0.01 sec.)
10. float (0.01 sec.)
11. double (0.01 sec.)
12. long double (0.02 sec.)

Adding elements to the end of the array (c-style array)

1. unsigned char (0.15 sec.)
2. unsigned long (0.15 sec.)
3. unsigned long long (0.15 sec.)
4. uint8_t (0.15 sec.)
5. uint64_t (0.15 sec.)
6. double (0.15 sec.)
7. unsigned short (0.16 sec.)
8. unsigned int (0.16 sec.)
9. uint16_t (0.16 sec.)
10. uint32_t (0.16 sec.)
11. float (0.16 sec.)
12. long double (0.48 sec.)

When to use std :: vector?

In large arrays, where the number of arithmetic operations is minimized, and where the operation of adding elements is abundantly applied. Otherwise, classical arrays are preferable.

Well, numeric types? Which is still faster, which is slower?

There is no exact answer here. According to the results, we can only say a few words about unsigned long long / uint64_t. These types are useful when you need to store numbers in a range greater than 4 bytes. In other cases, it is better to refuse, as performance drops and is very noticeable.

Source: https://habr.com/ru/post/265323/


All Articles