Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Allocate array to stack? [duplicate]

Tags:

arrays

c#

I am writing a program in C#. Everything centers around a static 2D int array which is 400x6 elements. Just a few values will be updated just once every minute. But after each minute's updates, dozens of functions will read the values millions of times to compute "pattern scores". The faster the calculations, the more distinct functions I can cram in there. Realistically I can allow 30 seconds for this scoring process. Is there a way to allocate the static array to the stack, and if so, would this help the speed? Thanks.

like image 660
OldRoboCoder Avatar asked Jul 25 '26 18:07

OldRoboCoder


1 Answers

Yes, you can alloc arrays on the stack in C# using "stackalloc" in "unsafe mode", but benchmarks shows a limited performance gain and the risk is that you hit the 1Mb stack size limit... which will give you a... StackOverflow(tm)!

Here is a good article on the subject: http://blogs.microsoft.co.il/sasha/2013/10/17/on-stackalloc-performance-and-the-large-object-heap/

like image 52
Dan Byström Avatar answered Jul 28 '26 12:07

Dan Byström



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!