I had problem with performance of "stack initialization". I am compiling my code with Visual Studio 2015
with optimization /O2
(speed).
See following structure:
template<typename T, size_t SIZE = 0>
TYPE SimpleArray;
template<typename T>
TYPE SimpleArray<T, 0> {
public:
size_t _size;
T * _ptr;
SimpleArray(T * ptr, size_t size) : _size(size) , _ptr(ptr) {}
SimpleArray() : _ptr(0), _size(0) {}
};
template<typename T, size_t SIZE>
TYPE SimpleArray : public SimpleArray<T> {
public:
T _array[SIZE];
SimpleArray() : SimpleArray<T>(_array, SIZE) {}
};
I tried to measure performance in following way:
template<size_t SIZE>
void test() {
Stopwatch stackWatch;
stackWatch.Start();
for (volatile size_t i = 0; i < 100000000; ++i) {
SimpleArray<char, SIZE> list;
}
stackWatch.Stop();
std::cout << "size\t" << SIZE << "\ttime\t" << stackWatch.ElapsedMilliseconds() << std::endl;
}
And I profiled it for sizes smaller than 1MB:
template<size_t SIZE = 1>
void invokeTests() { test<SIZE>(); invokeTests<SIZE * 2>(); }
template<>
void invokeTests<(1 << 20)>() {}
int main() {
invokeTests();
return 0;
}
if I use class:
#define TYPE class
I obtain results which I am not expecting:
size 1 time 204
size 2 time 187
size 4 time 203
size 8 time 219
size 16 time 187
size 32 time 250
size 64 time 688
size 128 time 1422
size 256 time 1719
size 512 time 1859
size 1024 time 2265
size 2048 time 3422
size 4096 time 6063
size 8192 time 10812
size 16384 time 20203
size 32768 time 40953
size 65536 time 141813
...
if I use struct:
#define TYPE struct
I obtain following results, which is what I am expecting:
size 1 time 203
size 2 time 187
size 4 time 188
size 8 time 187
size 16 time 203
size 32 time 203
size 64 time 188
size 128 time 187
size 256 time 188
size 512 time 187
size 1024 time 188
size 2048 time 187
size 4096 time 203
size 8192 time 188
size 16384 time 187
size 32768 time 188
size 65536 time 187
...
I know what causes performance drop. The performance drop what about class is caused by "zeroing" all the _array
element.
But why is that? Is it bug or expected behavior?
Edit: I found out that this happens if the compiler option /sdl /GS
is set. However there is still same question: why VS does difference between structs and classes?