nicht jedesmal end aufrufen
"Premature optimization is the root of all evil"
Diese "Verbesserung" führt (im Release Modus) bei vectors mit 10000, 512 Byte großen, Elementen im Durchschnitt zu einer Differenz von 0 Millisekunden... (manchmal ist sogar die "normale" Version schneller...)
Im Debug-Modus ist dein Vorschlag zwar deutlich schneller, aber man optimiert ja schließlich nicht für bessere Debug-Geschwindigkeit...
Falls du's selber austesten willst:
|
C-/C++-Quelltext
|
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
34
35
36
37
38
39
|
#include <iostream>
#include <Windows.h>
#include <vector>
#undef max
struct HugeTestStruct
{
char test_array[512];
};
int main()
{
std::vector<HugeTestStruct> test_vector(10000);
__int64 total_diff = 0;
const int count = 5000;
for(unsigned int i = 0; i < count; ++i)
{
__int64 t_s = timeGetTime();
for(std::vector<HugeTestStruct>::iterator it = test_vector.begin(); it != test_vector.end(); ++it)
*it = HugeTestStruct();
__int64 t_diff = timeGetTime() - t_s;
t_s = timeGetTime();
for(std::vector<HugeTestStruct>::iterator it = test_vector.begin(), end = test_vector.end(); it != end; ++it)
*it = HugeTestStruct();
__int64 t_diff_2 = timeGetTime() - t_s;
std::cout << "nicht 'optimiert' : " << t_diff << std::endl;
std::cout << "'optimiert' : " << t_diff_2 << std::endl;
std::cout << "Differenz : " << t_diff - t_diff_2 << std::endl << std::endl;
total_diff += t_diff - t_diff_2;
}
std::cout << "Durchschnittliche Differenz: " << total_diff / count;
std::cin.get();
return 0;
}
|