简体   繁体   中英

Stack and Heap performance?

I ran a small test using C# using struct and class ... executed a simple method on each (console app) with WriteLine and used System.Diagnostics.StopWatch to measure.

My expectation was struct would be faster, but the results show them to be almost identical in performance.

There has been a lot of emphasis on stack vs. heap with the provision that heap has more processing overhead ... I've been fairly vigilant with my code thinking the stack is where I want to be if design construct fits.

So is there something else happening in the Virtual Execution System (aka CLR) that's doing some optimization that trivializes the stack and heap?

I must admit, I've never actually thought about testing this before, maybe I just need a more complex test to really see the difference?

Thoughts?

Cheers, Rob.

Premature optimisation.

You don't choose heap/stack based on speed. You pick it based on how much memory you're going to need. The only extra overhead is the time to allocate/deallocate heap memory but that is trivial in the grand scheme of things.

You've already spent more time on this than it needs :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM