Arrays, Jagged arrays, multidimensional arrays Performance [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'll make it short:
int[,,,] Stats = new int [6, 4, 30, 10];
This will have 6 * 4 * 30 * 4 = 7200 elements, right?
And performance should be as good as it gets right?
If I loop through that and look for specific numbers by
if (x = y)
this should be faster than doing that with List or other things, right, because internally all is handled like the basic array, if I understand that all correctly?

Arrays are constant, vs List that is dynamic, which means when you make a new array, c# allocate memory for all those elements (in this case 7200x(int size)).
In a list, whenever you make a new entry, c# has to update the list, allocate new memory for your new int and "connect" it to the list, therefore will be slower always.
SO, if you don't care about "wasting" memory, array will be your best choice for performance.

Related

foreach loop with 2 hashset variables c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I have the following
variableAs = "A12,B12,C12"
variableBs = "1.54,75.30,55.50"
method (HashSet<string> variableAs, HashSet<double> variableBs)
foreach (var variableA in variableAs)
{
Method here requires the two values, must have preserved datatype
and be in same order, ie A12 with 1.54, B12 with 75.30
}
I have tried zip from this answer but I do not know how it works with hashset arrays,
NOTE the Method i am editing has it has hashset, the actual values are for example only, if there is an error, It must be my understanding of what a hashset is but I cannot change the hashset
You need to first populate your input-strings into some collection:
var As = variableAs.Split(',');
var Bs = variableBs.Split(',');
However a HashSet is not an ordered collection and thus the wrong choice here. You'd need a List or just an array.
Now you may use the mentioned Zip-function to combine the two elements together:
var result = As.Zip(Bs, (a, b) => new { a, b });
See the complete code at this fiddle: https://dotnetfiddle.net/9qTG2E

Differences between ways of creating ImmutableList [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 months ago.
Improve this question
What is the differences between ways of creating ImmutableList?
List<int> numbers = new List<int>(){0, 1, 2, 3};
ImmutableList<int> immutableList = numbers.ToImmutableList();
ImmutableList<int> immutableList = ImmutableList.Create<int>(0, 1, 2, 3);
ImmutableList<int> immutableList = ImmutableList.CreateRange<int>(new List<int>() { 0, 1, 2, 3 });
ImmutableList<int>.Builder builder = ImmutableList.CreateBuilder<int>();
builder.AddRange(new List<int>() { 0, 1, 2, 3 });
ImmutableList<int> immutableList = builder.ToImmutableList();
Which is the faster and usabel?
Let's take a look at how each of those is implemented.
First off, the ToImmutableList() extension method involves first trying to cast to ImmutableList<T>, and then falling back to ImmutableList<T>.Empty.AddRange() otherwise. ImmutableList.Create() and ImmutableList.CreateRange() also call ImmutableList<T>.Empty.AddRange().
Finally, the builder is the only one that has some key difference, although you're using it wrong: you should be using ToImmutable(), because it involves doing fewer unecessary copies. ToImmutableList() just uses the above extension method, and therefore also uses ImmutableList<T>.Empty.AddRange().
Really, if you're building a possibly large immutable list as you go, you should use a builder, and then freeze it with ToImmutable() when you're done modifying it. Otherwise, all the other methods are functionally identical, so pick whichever is clearest to you.

Why does foreach allocate int arrays in C#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Why does Rider DPA say that this code allocates int arrays?
foreach doesn't itself allocate anything (except space for some locals, which aren't heap allocations in this case). It is roughly equivalent to:
int result = 0;
using (IEnumerator<int> iter = numbers.GetEnumerator()) {
while (iter.MoveNext()) {
var number = iter.Current;
result |= 1 << (number - #base);
}
}
return result;
As you can see: it has literally no direct allocations, but whatever you pass in as numbers has 4 opportunities to do anything it chooses: GetEnumerator(), MoveNext(), Current, and Dispose(). We can't tell you what the input numbers is, but there's a good chance that the tool is measuring these invisible allocations and attributing them to the foreach.
Look at whatever numbers is.

How to input a two-dimensional array in one loop? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
My teacher gave me today this mission, to built a code block in c# that input data to a two-dimensional array, in only one loop.
What is the easiest way to do so? I tried some thing with While and they didn't work at all.
You can create a counter that is the length of the first array, a counter for the length of the second array. Then increment each as you seed the arrays appropriately. I'm not sure you want actual code since this is an assignment.
You can use a while loop that checks for the counters to be a certain length to know when the arrays are finished being loaded with data.
I suspect your teacher wants to learn about modulo division operator (%). If your two dimensions are of sizes X & Y respectively, then you have a total of X*Y items in your 2D array. So you can always translate item's count into it's position in 2D array. E.g. (in pseudo code):
for(int i = 0; i < X*Y; ++i)
{
myArray[i%x, i/x] = i;
}

Adding values from textbox to 2-d array or list? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
i have a form where values from textbox need to be stored in an 2-d array
the values are cloth length, quantity and cut value, which are entered into a listbox, so while displaying these values into labels while selecting from listbox, i use the array.
the problem is there any other data structure than 2-d array like list or the collection classes which can be useful. removing values from 2 d array is too much code every time.
You could use one of these:
List<Cloth>
Dictionary<string, Cloth>
There are no problems to remove items from array (by index I suppose):
array = array.ToList().RemoveAt(...).ToArray();
But if you plan to have many of such, then you should, as RononDex suggested, use List-based storage from start.

Categories

Resources