how to create an ip range fast as fast as possible? [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
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.

Related

Why should I use continue as opposed to an empty if statement? [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 7 years ago.
Improve this question
For example:
What is the disadvantage/advantage of using this block:
for(int i = 0; i < 10; i++)
{
if(i < 9){}
else Console.Writeline(i);
}
As opposed to this block:
for(int i = 0; i < 10; i++)
{
if(i < 9)
{
continue;
}
Console.Writeline(i);
}
Considering their output is exactly the same.
Empty blocks are weird, so in this case, you would just invert the condition:
for (int i = 0; i < 10; i++)
{
if (i >= 9)
Console.Writeline(i);
}
But in the general case, using continue or break is a good way to prevent your code from being indented too many levels. For some discussion on that topic, see this question on Programmers SE.
I always do
// ...
if(!(i < 9))
Console.Writeline(i);
// ...
to prevent an empty if statement.
In your code, there is little difference between the two, with the first it doesn't reach the else statement, and the second it skips over the rest in the loop.
The only reason I find to chose one over the other is legibility. I personally use both forms, opting for one or another based more on the length of the block after the if. For example, if it contains a few lines only I just use the plain if:
for (int i = 0; i < 10; i++)
{
if (i >= 9)
Console.Writeline(i);
}
(example taken from poke answer).
But if the work is considerable, I would invert the condition and rely on continue instead:
for (int i = 0; i < 10; i++)
{
if (i < 9) continue;
Console.Writeline(i);
Console.Writeline("this");
Console.Writeline("that");
Console.Writeline("blablabla");
Console.Writeline("hello");
Console.Writeline("world");
Console.Writeline(i + 1);
Console.Writeline(i + 2);
}
Doing so avoids the indentation on a much bigger block. Doing on a single or very few lines is generally easy to read, but on a longer block can become more difficult to follow.

Is this implementation bad programming? [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 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.

Arrays In loops by 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 9 years ago.
Improve this question
I want to make a lot of array in for loops by C# like below: please help me!
for(int i=1;i<10;i++)
{
int[][] a+i=new int[10][3];
}
IMO, best will be to use a Dictionary<string,int[][]>.
During creation you will place a new array (which you just created) and assosiate it to the key "a" + i.
To get this array, just get the value attached to the relevant key.
Something like (C#-like pseudo code):
var map = new Dictionary<string,int[][]>();
for(int i=1;i<10;i++)
{
var temp = new int[10][3];
map["a" + i] = temp
}
and to get a value just use map[key] (for example map["a7"] will get the 7th element).
A good alternative would be to use a 3D array.
You are probably trying to create jagged arrays. Here's how you can do it:
var a = new int[10][];
for (var i = 0; i < a.Length; i++)
a[i] = new int[3];
for (var i = 0; i < a.Length; i++)
for (var j = 0; j < a[i].Length; j++)
a[i][j] = 1; // Initialize with your values

Multiplication or if: what is more efficient? [closed]

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.

How to sort array without using sort method in C# [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
How to sort array without using sort method in C#
Visualization and Comparison of sorting algorithms in C#
example code
public IList BubbleSort(IList arrayToSort)
{
int n = arrayToSort.Count - 1;
for (int i = 0; i < n; i++)
{
for (int j = n; j > i; j--)
{
if (((IComparable)arrayToSort[j - 1]).CompareTo(arrayToSort[j]) > 0)
{
object temp = arrayToSort[j - 1];
arrayToSort[j - 1] = arrayToSort[j];
arrayToSort[j] = temp;
}
}
}
return arrayToSort;
}
above will surely helps you to understand the thing you want
By assigning value by value in alphabetical or whatever order.
Sorry, but I don't get your question. What is the actual problem you are trying to solve?
Use nested loop sorting methods such as bubble sort.
But why would you want to do such a thing?
Since the array implements IEnumerable<T>, you can use the OrderBy extention method on IEnumerable to sort it.
If your question is how to do it without using any built in functionality in the framework, I guess I will have to ask. Why??

Categories