Re: object on stack/heap performance problems
orobalage@gmail.com wrote:
Hi!
I was developing some number-crunching algorithms for my university,
and I put the processor into a class.
While testing, I found a quite *severe performance problem* when the
object was created on the stack.
I uploaded a test archive here: http://digitus.itk.ppke.hu/~oroba/stack_test.zip
Inside you'll find the number cruncher class (CNN in cnn.h and
cnn.cpp), as well as two test files: test_slow.cpp and test_fast.cpp.
They differ ONLY in where the processor object is created. In one, it
is created on the stack, in the other, it is created on the heap. Yet,
when I call the member function process(), the performance difference
is 5x!!!
Can someone with a higher knowledge of object layout and whatsoever,
tell me why this is happening?
--
Thanks in advance,
B.
Well, it seems that I cannot reproduce what you just described:
benben@watersidem $ g++ main_fast.cpp cnn.cpp -O2 -o fast
benben@watersidem $ g++ main_slow.cpp cnn.cpp -O2 -o slow
benben@watersidem $ ./fast
520000
benben@watersidem $ ./slow
520000
Theoretically there shouldn't be any difference between performance of
operations on an object on the stack and the same operation on an object
on the heap. At least on my machine I cannot reproduce such difference.
In a highly unlikely event the stack memory may be swapped out before
the process() call, resulting in swapping back in the memory. But this
is unlikely judging the straightforward manor of your program, plus the
swapping can happened to heap memory equally likely anyway...
Regards,
benben