Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I have a binary image (size: 100x100) of a hand, that we can represent as a matrix composed only by 0 or 1 values. This is an example:
Assuming that I have an array of double representing the linearized image, and that we call it image, I need to perform the following operations:
double accumulator = 0;
for (int j = 0; j < image.Length; ++j)
{
accumulator += image[j] * weights[j];
}
In other words, I need to calculate the weighted sum of each pixel of the image array. weights represents an array that contains double values, and it is used to weight each pixel of the image.
Is the following code more efficient than the previous one?
double accumulator = 0;
for (int j = 0; j < image.Length; ++j)
{
if (image[j] != 0)
{
accumulator += image[j] * weights[j];
}
}
The answer is - you really don't need to be worrying about this at this juncture. If your code is running too slow, then you can experiment with some optimization. However, "too slow" is subjective.
Premature Optimization Is The Root Of All Evil.
As angelatlarge points out, for the moment - go with what is easily readable, and easily maintainable.
Related
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
Write a method:
public int IsXDivisibleByY(int X, int Y) { .. }
That meets the following conditions:
Method returns 1, if X can be divided by Y without a remainder.
Method returns 0, if X can't be divided by Y without a remainder.
Solution MUSN'T include any conditional checks (if condition, equality operators, the ternary and null-coalescing operators, etc.).
Solution MUSN'T include any exception handling (try-catch-finally)
It must be taken into account that both parameters could be given all range of the parameter type.
The answer is going to be considered wrong if any of the aforementioned conditions is not met.-
Using C#.
Here is my solution, it uses only modulus and bit operations (it also uses conditional checks in for loop, but it can be easily replaced by 32 explicit code blocks, I will omit this for better looking).
public int IsXDivisibleByY(int X, int Y)
{
var mod = (int)((float) X % (float) Y);
var accum = 0;
for (var i = 0; i < 32; i++)
{
accum = accum | (mod & 1);
mod = mod >> 1;
}
return accum ^ 1;
}
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
I am trying to find at what index a sine curve (which may start at any point along the curve) reaches its first maximum, and only the first. To do this, I am running a loop which compares one value to its previous. If one point is greater than its previous value, it is trending up, and similar for the opposite.
In c#, how do you detect when the variable has changed from trending up to trending down? In other words, how do you detect when the variable has changed. In LabVIEW, this can be done using a shift register. What is the equivalent in c#?
public static int FirstMaxIndex(int[] values)
{
bool up = false;
for (int i = 1; i < values.Length; i++)
if (values[i] < values[i - 1])
{
if (up) return i;
else up = false;
}
else if (values[i] > values[i - 1])
{
up = true;
}
return -1;
}
I didn't test this. This is only to give you an idea of how to solve this. (I wrote it as close as possible to what you wrote in a comment.)
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
how to get the fastest result
i write the code below.
for (int i = 0; i < 256; i++)
for (int j = 0; j < 256); j++)
for (int k = 0; k < 256; k++)
for (int p = 0; p < 256; p++)
{
writer.WriteLine(string.Format("{0}.{1}.{2}.{3}", i, j, k, p));
}
but my users told me that it is dammed slow. i dont have any idea how to boost the progress. share the problem, maybe
someone knows that. thanks.
You can try with IPAddressRange : https://www.nuget.org/packages/IPAddressRange/
But it will still be very long if you want to get all the ipv4 range!
var range = NetTools.IPAddressRange.Parse("192.168.0.10 - 192.168.10.20");
System.Text.StringBuilder builder = new System.Text.StringBuilder();
foreach (var item in range)
{
builder.Append(item);
}
You are saving 256 to the power of 4 items. That is over 4 billion calls to setText. If you need to create that many items, you have to look into the performance of setText. Your loop is performant enough and if you can optimize setText is unclear because I don't know what it does. But anything you do 4 billion times will be probably slow.
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
I made the algorithm below for bubblesort exercise at school.
//Bubble Sort (My code)
static void _BubbleSort(int[] a)
{
for (int i = 0; i < a.Length - 1; i++)
{
for (int j = 0; j < a.Length - 1 - i; j++)
{
if (a[j] > a[j + 1])
{
swap(a, j);
}
}
}
}
But when I check the internet I see different algorithm below.
The algorithm I found in internet uses different "for" loop as follows. Mine is nested for loops but the code below is not nested.
public void BubbleSort(int[] b)
{
for (int pass = 1; pass < b.Length; pass++) // passes
for (int i = 0; i < b.Length - 1; i++) // one pass
if (b[i] > b[i + 1]) // one comparison
Swap(b, i); // one swap
}
What I want to ask is my code is an example of bad programming or not? or my brain is working different than you computer science guys? I am arts student by the way if you wonder.
What I want to ask is my code is an example of bad programming or not?
Your code almost is identical. There is a difference in that you are (correctly) using curly brackets to explicitly state code blocks, while the other example isn't. One problem is your loop bounds check.
#Sriram also pointed to the fact that your loop uses a.Length - 1 - i while the latter simply checks for b.Length - 1, which isn't actually necessary on your part and would cause the loop to prematurely end. Use the latter approach from the second example.
Other than the fact you shouldn't use _ at the beginning of your method (this is simply a naming convertion), your algorithm is identical.
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'm making a game that involves finding and clicking on a number (0-9) in a grid that randomizes each time you click on the correct one.
I want to get it so that when you click on the correct number, the grid randomizes again.
How would you do this?
Here's what it would kind of look like in the end:
I assume you're rendering an array of integers in order:
for (int i = 0; i < arrayOfNumbers.Length; i++ ) {
// rendering here
render(arrayOfNumbers[i]);
}
If thats the case.. just randomize the array after a successful click.. somewhat like this:
var rnd = new System.Random();
var arrayOfNumbers = Enumerable.Range(1, 9).OrderBy(r => rnd.Next()).ToArray();
Then you can just re-render (or let your game loop continue to render the array). Since the array has changed, your rendering will too.
Every time you detect a click on the correct number (I hope you know how to do this) you simply randomize the array of numbers you're displaying in your grid:
//Fisher-Yates algorithm
Random generator = new System.Random();
int len = array.Length;
while (len > 1)
{
len--;
int k = generator.Next(len + 1);
int temp = array[k];
array[k] = array[len];
array[len] = temp;
}