Why does foreach allocate int arrays in C#? [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 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.

Related

Is using the same name for LINQ Queries range and iteration variables good practise? [closed]

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 3 years ago.
Improve this question
At “MSDN” it says “The range variable is like an iteration variable in a foreach statement except for one very important difference: a range variable never actually stores data from the source. It's just a syntactic convenience that enables the query to describe what will occur when the query is executed."
The example at Introduction to LINQ Queries (C#)” below is one of the few where the range and iteration variables have the same name (“num”)
// 2. Query creation.
var numQuery =
from num in numbers
where (num % 2) == 0
select num;
// 3. Query execution.
foreach (int num in numQuery)
{
Console.Write("{0,1} ", num);
}
Usually MSDN examples assign the range and iteration variables different names.
Are there any reasons not to use the same name for both the range variable and iteration variable when it seems convenient to do so?
At the step 2 you're building a query and at the 3 step you've already executed the query,
so the (1st)num var didn't exist at the same time to the (foreach)num var
technically both num var represent the same concept, so you can use as you want.
Generally devs user 'item' for range variable
if the foreach statement is long and complicated, i use your naming convention
...otherwise => 'item'

What does <+> mean? [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 5 years ago.
Improve this question
I'm reading some documentation (under NDA) where is says to do the following operation:
Rnd1[11..7] <+> Rnd2[11..7]
Rnd1 and Rnd2 represent an array of bytes.
What should operator <+> do here?
I've browsed through entire documentation and can't find an explanation.
It is likely an attempt to make an ASCII representation of the symbol ⊕, that symbol represents XOR.
So the documentation is likely telling you to take bytes 11 through 7 of Rnd1 and xor them with the same bytes in Rnd2
public byte[] YourOperator(byte[] rnd1, byte[] rnd2)
{
byte[] result = new byte[5];
for(int i = 7; i <= 11; i++)
{
result[i - 7] = rnd1[i] ^ rnd2[i];
}
return result;
}
Be careful to check if the 7..11 is a inclusive or exclusive upper bound, that will change the number of bytes you are working with from a 5 to a 4 and the <= to a <.
The fact you signed a NDA to get the documentation means you should have working relationship with the company that gave it to you, the best option is to contact them and ask for clarification so you know for sure what they meant.
I like the Linq way of doing things, e.g.
rnd1.Zip(rnd2, (r1, r2) => r1 ^ r2).Skip(7).Take(4);

Arrays, Jagged arrays, multidimensional arrays Performance [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 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.

Interlocked.Increment at the end of begining of the parallel loop? [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 years ago.
Improve this question
Suppose you have a parallel loop in C# and want to use : Interlocked.Increment
Also, suppose there is a heavy process in your loop. Do you think putting Interlocked.Increment before or after that heavy process might make any difference?
In other words, which of the followings is more preferred?
UPDATE: Suppose our criterion for being better or worse is the overall speed.
Prog1:
int iter = 0;
Parallel.For(...{
HeavyProcess()
Interlocked.Increment(iter);
});
Prog2:
int iter = 0;
Parallel.For(...{
Interlocked.Increment(iter);
HeavyProcess()
});
The "one that is preferred" is simply: the one that is functionally correct. Is it meant to record the number of complete operations (end)? or the number of started operations (beginning). Other than that: no difference.

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;
}

Categories

Resources