Copy speeds (as reported by MSCV++ 6.0 Professional’s Profiler)
Using an array of 16 floats (10,000 copies)
Fastest – a[0] = b[0]; a[1] = b[1] …… etc.
Middle – memcpy(a, b, 64);
Slowest – a[y] = b[y];
Profiler results
Function Time |
% of time |
Func + Child Time |
% of time |
Hit count |
Function |
1.807 |
73.2 |
1.807 |
73.2 |
1 |
T3(void) |
0.514 |
20.8 |
0.514 |
20.8 |
1 |
T1(void) |
0.147 |
5.9 |
0.147 |
5.9 |
1 |
T2(void) |
0.002 |
0.1 |
2.469 |
100.0 |
1 |
_main |
Conclusion: don’t use memcpy in matrix/vector classes.
Code used:
int main() { T3(); T2(); T1(); return 0; } void T1() { for(int x = 0; x < 5000; x++) { memcpy(b, a, 64); memcpy(a, b, 64); } } void T2() { for(int x =0; x < 5000; x++) { b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; b[3] = a[3]; b[4] = a[4]; b[5] = a[5]; b[6] = a[6]; b[7] = a[7]; b[8] = a[8]; b[9] = a[9]; b[10] = a[10]; b[11] = a[11]; b[12] = a[12]; b[13] = a[13]; b[14] = a[14]; b[15] = a[15]; a[0] = b[0]; a[1] = b[1]; a[2] = b[2]; a[3] = b[3]; a[4] = b[4]; a[5] = b[5]; a[6] = b[6]; a[7] = b[7]; a[8] = b[8]; a[9] = b[9]; a[10] = b[10]; a[11] = b[11]; a[12] = b[12]; a[13] = b[13]; a[14] = b[14]; a[15] = b[15]; } } void T3() { for(int x = 0; x < 5000; x++) { for(int y = 0; y < 16; y++) { a[y] = b[y]; } for(y = 0; y < 16; y++) { b[y] = a[y]; } } }