开发者

Improve the code for performance?

开发者 https://www.devze.com 2023-04-11 17:35 出处:网络
I wrote the follow c# codes to generate a set of numbers and then compare with another set of numbers to remove the unwanted numbers.

I wrote the follow c# codes to generate a set of numbers and then compare with another set of numbers to remove the unwanted numbers.

But its taking too long at run time to complete the process. Following is the code behind file.

The numbers it has to generate is like 7 figures large and the numbers list which I use it as to remove is around 70开发者_如何学JAVA0 numbers.

Is there a way to improve the run time performance?

string[] strAry = txtNumbersToBeExc.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
    int[] intAry = new int[strAry.Length];
    List<int> intList = new List<int>();
    for (int i = 0; i < strAry.Length; i++)
    {
        intList.Add(int.Parse(strAry[i]));
    }

    List<int> genList = new List<int>();

    for (int i = int.Parse(txtStartSeed.Text); i <= int.Parse(txtEndSeed.Text); i++)
    {
        genList.Add(i);
    }
    lblStatus.Text += "Generated: " + genList.Capacity;

    var finalvar = from s in genList where !intList.Contains(s) select s;

    List<int> finalList = finalvar.ToList();

    foreach (var item in finalList)
    {
        txtGeneratedNum.Text += "959" + item + "\n";
    }


First thing to do is grab a profiler and see which area of your code is taking too long to run, try http://www.jetbrains.com/profiler/ or http://www.red-gate.com/products/dotnet-development/ants-performance-profiler/.

You should never start performance tuning until you know for sure where the problem is.

If the problem is in the linq query than you could try sorting the intlist and doing a binary search for each item to remove, though you can probably get a similar behavour with the right linq query.


    string numbersStr = txtNumbersToBeExc.Text;
    string startSeedStr = txtStartSeed.Text;
    string endSeedStr = txtEndSeed.Text;


    //next, the input type actually is of type int, we should test if the strings are ok ( they do represent ints)
    var intAry = numbersStr.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Select(s=>Int32.Parse(s));   
    int startSeed = Int32.Parse(startSeedStr);
    int endSeed = Int32.Parse(endSeedStr);

    /*FROM HERE*/
    // using Enumerable.Range 
    var genList = Enumerable.Range(startSeed, endSeed - startSeed + 1);

    // we can use linq except
    var finalList = genList.Except(intAry);

    // do you need a string, for 700 concatenations I would suggest StringBuilder
    var sb = new StringBuilder();
    foreach ( var item in finalList)
    {
        sb.AppendLine(string.Concat("959",item.ToString()));
    }
    var finalString = sb.ToString();
    /*TO HERE, refactor it into a method or class*/

    txtGeneratedNum.Text = finalString;

They key point here is that String is a immutable class, so the "+" operation between two strings will create another string. StringBuilder it doesn't do this. On your situation it really doesn't matter if you're using for loops, foreach loops, linq fancy functions to accomplish the exclusion. The performance hurt was because of the string concatenations. I'm trusting more the System.Linq functions because they are already tested for performance.


  1. Change intList from a List to a HashSet - gives much better performance when determining if an entry is present.
  2. Consider using Linq's Enumerable.Intersect, especially combined with #1.


Change the block of code that create genList with this:

List<int> genList = new List<int>(); 

    for (int i = int.Parse(txtStartSeed.Text); i <= int.Parse(txtEndSeed.Text); i++) 
    { 
        if (!intList.Contains(i)) genList.Add(i); 
    } 

and after create txtGeneratedNum looping on genList. This will reduce the number of loop of your implementation.


Why not do the inclusion check when you are parsing the int and just build the result list directley.

There is not much point in iterating over the list twice. In fact, why build the intermediate list at all !?! just write straight to a StringBuilder since a newline delimited string seems to be your goal.

string[] strAry = txtNumbersToBeExc.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

var exclusions = new HashSet<T>();
foreach (string s in txtNumbersToBeExc.Text.Split(new string[] { Environment.NewLine })
{
    int value;
    if (int.TryParse(s, value)
    {
        exclusions.Add(value);
    }
}


var output = new StringBuilder();
for (int i = int.Parse(txtStartSeed.Text); i <= int.Parse(txtEndSeed.Text); i++)
{
    if (!exclusions.Contains(i))
    {
        output.AppendFormat("959{0}\n", i);
    }
}

txtGeneratedNum.Text = output.ToString();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号