Interlocked.Increment at the end of begining of the parallel loop? [closed] - c#

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.

Related

For loop does not finish [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 11 months ago.
Improve this question
I am making a game in Unity, 2D and for the creation of levels, I have added a for loop so that a number of blocks is generated a certain number of times. The problem is that it never ends, that is, instead of ending on the game screen when the two blocks are created, it continues to generate blocks infinitely.
public void GenerateInitialBlocks()
{
for (int i = 0; i < 2; i++)
{
AddLevelBlock();
}
}
I have reset the Script in Unity because it usually gives compilation errors or crashes, but it still doesn't work. Thanks for read.
I believe the problem could be in how many times the method is called.
find where in the code the method is called (perhaps using search)
make sure it's called only once per level and not repeatedly when refreshing or repainting (scene updating methods).

Convert.ToInt32("example") Collisions? [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 5 years ago.
Improve this question
Will the C# function Convert.ToInt32(text) convert different words to the same int? Any links or pushes in the right direction are appreciated!
P.S.
What about anagrams?
No, Convert.ToInt32(text) will just try to parse your text to an int, like:
Convert.ToInt32("032") will return 32 as int but
Convert.ToInt32("Brian") will throw an exception.
I assume that you want to have some kind of hashing, when you say "different words to the same int".
Try GetHashCode(). It will return the same value if you call it multiple times with the same value, for example:
"Brian".GetHashCode() will always return 1635321435

1000 same chars without using loops and repeating this char in code more than once [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 7 years ago.
Improve this question
Let's say we need to output 1000 # symbols in one line without loops or recursion. And surely without making a line of code with 1000 chars there.
The noob solution that comes to mind is...
string s = "#";
s = s+s+s+s+s+s+s+s+s+s;
s = s+s+s+s+s+s+s+s+s+s;
s = s+s+s+s+s+s+s+s+s+s;
Console.WriteLine(s);
Are there any others?
var repeatedString = new string('#', 1000);

Counting frequency of characters in a line from a .txt file [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
I have a C# task to do and I am stuck on part of the coding. I am using StreamReader to read a .txt file which contains group exam grade data (e.g. ADCBCBBBADEBCCBADBAACDCCBEDCBACCFEABBCBBBCCEAABCBB), I need the code to work out how many A's, B's etc there are inside each set of data, I thought about using some form of count++ code but each attempt just throws errors.
I want it to print onto console the number of A's in that line of the .txt file.
I hope that makes sense, I understand how to do the rest but I just needed a hand on this section.
Consider using System.Linq, eg...
string myString = "ADCBCBBBADEBCCBADBAACDCCBEDCBACCFEABBCBBBCCEAABCBB";
int countOfAs = myString.Count(x => x == 'A');
//Result: 9

How Can I Assign Multiple Values To A Void [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 8 years ago.
Improve this question
How Can I Assign Multiple Values To Where It Says uint i need to to be from 0 to 18 because im doing something client based a it needs to be done for all 18 clients at once
RPC.doTypeWriter((uint) 0 , (int)numericUpDown21.Value, metroTextBox22.Text, (short)numericUpDown23.Value, (double)numericUpDown24.Value, (float)numericUpDown25.Value, (float)numericUpDown26.Value, (ushort)numericUpDown35.Value, (ushort)numericUpDown36.Value, (ushort)numericUpDown37.Value, (int)numericUpDown27.Value, (int)numericUpDown28.Value, (int)numericUpDown29.Value, (int)numericUpDown30.Value, (int)numericUpDown31.Value, (int)numericUpDown32.Value, (int)numericUpDown33.Value, (int)numericUpDown34.Value);
You can't. An uint is a single integer value, it can't hold multiple values.
Use a loop to loop from 0 to 17 (as that is 18 clients):
for (uint i = 0; i < 18; i++) {
RPC.doTypeWriter(i, (int)numericUpDown21.Value, metroTextBox22.Text, (short)numericUpDown23.Value, (double)numericUpDown24.Value, (float)numericUpDown25.Value, (float)numericUpDown26.Value, (ushort)numericUpDown35.Value, (ushort)numericUpDown36.Value, (ushort)numericUpDown37.Value, (int)numericUpDown27.Value, (int)numericUpDown28.Value, (int)numericUpDown29.Value, (int)numericUpDown30.Value, (int)numericUpDown31.Value, (int)numericUpDown32.Value, (int)numericUpDown33.Value, (int)numericUpDown34.Value);
}
The naming suggests that you are doing RPC calls to the clients, so you might need to move more code into the loop, e.g. doing the connection to each client in the loop.

Categories

Resources