This question already has an answer here:
How to allocate arrays on the stack for performance gains?
(1 answer)
Closed 7 years ago.
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.
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/
You can use "stackalloc" to alloc array directly on the stack.
Some documentation about :
https://msdn.microsoft.com/en-us/library/aa664785(v=vs.71).aspx
You can also use an implementation of Hamming weight which is describe here :
How to allocate arrays on the stack for performance gains?
Related
This question already has answers here:
What is the maximum possible length of a .NET string?
(8 answers)
Closed 4 years ago.
I know in C# you can do int.MaxValue or long.MaxValue to get the maximum for these two types, I was wondering if there are similar ways to get the maximum length for the string.
I don't see it here.
Also, I am aware that there are already questions being asked on max string length. Max string length in C#
I am asking if there is an existing function that comes in handy where you can use it to set the condition when and when not to truncate your string to avoid program crashing.
Or what is the normal approach?
E.g. Maybe something as easy as Max(string.length)?
No. In the same way that there is no known method to find the max length of an array you can allocate. Until you try to allocate the memory, you can't know if there is enough virtual address space and enough physical memory to contain it. Theorically you could VirtualAlloc (Windows API) greater and greater memory blocks until it fails, deallocate the block and then try to allocate the same amount of memory in .NET, knowing that the memory is there so the .NET should be able to allocate it.
Note that this is true at 32 bits... I haven't ever seen an out-of-memory error at 64 bits.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Basically, I'm wanting to efficiently pair multiple bool and bytes ( to store values ) with a pre-made class (RenderTarget2D if you must know).
Obviously, I can wrap this all in a class, however there are situations when I will be having many of these, and would like to save on memory where possible (ie use a struct).
I know it's bad behaviour to use a struct with reference variables, and I'd prefer not to just use separate variables to hold the information ( would rather pair it all together ).
Essentially I am wanting a structure to hold a reference to a class, a bool, and a byte, and create a 2D array of this to make many (thus am looking for a mitigate memory usage)
Am I overlooking an obvious solution?
Understanding the question as:
You want something that holds a bool, a byte and an instance of the class RenderTarget2D.
If that is the case you can use Tuple<bool, byte, RenderTarget2d>.
Creating a custom class or struct is also a viable option. In fact there is a proposal for "C# 7" to include language native tuples (that will not be System.Tuple) and as currently written, they will be structs.
You may also want to consider that having a reference to the RenderTarget2d may prolong its lifespan.
Struct vs Class
The struct takes in memory (when compacted) the size of a bool plus the size of a byte plus the size of a reference (to RenderTarget2d). If you have an array of 600 by 600 (360000) of such structs it takes 360000 the size of the struct in memory.
If you use classes, the array will have 360000 references to the actual location of the data that in total takes at least as much as the array of structs.
So using the structs should take less memory...
But when you take a struct from your data structure, you are actually making a copy. So each time you access your array to get a item and read a property of that item, you are actually making a copy of the item and reading the property from that copy.
If you want to update it, you need to read it (that makes a copy as mentioned above) edit it, and then put it back... and that copies the data to the array.
So, if the memory is the main concern. Use struct. To quote Jon Skeet: "so long as you're aware of the consequences".
Using struct also means less RAM round trips. Not only because it avoids resolving the references, but also because the data is guaranteed to be close together. This allows for better performance because the CPU will load a chunk (or the totality) of the data structure in cache and since the code is not using references outside of that it will be able to keep it in cache instead of going to load another thing.
This question already has answers here:
Are there any differences between Java's "synchronized" and C#'s "lock"?
(3 answers)
Closed 9 years ago.
I'm wondering if there is any difference in runtime at lock vs syncronized.
I have learnd that syncronized is a slow operation and outdated at Java.
Today I saw the lock at C# and I'm wondering if they are the same and lock is something I "want" to avoid same as in Java or maybe he is much faster and I want to use it...
Thanks!
1 synchronized is not outdated, java.util.concurrent.locks package simply provides extended functions which are not always needed.
2 Locking is done at CPU level and there is no difference between Java and C# in this regard
see http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html
... special instructions, called memory barriers, are required to flush or invalidate the local processor cache in order to see writes made by other processors or make writes by this processor visible to others. These memory barriers are usually performed when lock and unlock actions are taken; they are invisible to programmers in a high level language.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Array memory allocation - paging
Does byte[,] myArray is always in continuous memory block?
I have some code which works with images stored in such arrays, everything works fine, but I just ask for sure.
EDIT: I work with unsafe code.
Yes, it's always there, in memory and will be memorized consecutively.
take a look : True Unsafe Code Performance
And msdn says you can use without think about it: http://msdn.microsoft.com/en-us/library/28k1s2k6%28v=vs.80%29.aspx
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Are C# arrays thread safe?
I have a program that is single-threaded at the moment, which basically puts lots of computed data into a multidimensional array (e.g. double[,] ; string[,] ).
Is it possible to assign segments of this array to different threads ? More precisely, if I make sure only one thread will write at a given coordinate, will there be some lock mechanism triggered ?
In terms of concurrency problems, you will be fine as long as your threads do not read or write to the same portion of your array concurrently. You may see slowdowns because of "False Sharing" hazard, though, so you may want to be on the lookout for unexpected slow-downs when the number of threads increases.
if I make sure only one thread will write at a given coordinate
Then you are safe. Assuming you don't resize the array etc.
If you are now using a for loop you can probably simply switch to Parallel.For(0, n, method)