.net用于循环性能问题

时间:2015-05-28 07:43:15

标签: c# .net performance

我有这段代码:

namespace FrequencyGeneratorConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread.CurrentThread.Priority = ThreadPriority.Highest;

            int from = 100000;// Convert.ToInt32(txtFrom.Text);
            int to = 999999;//Convert.ToInt32(txtTo.Text);
            int numbers = 2000; //Convert.ToInt32(txtNumbers.Text);
            int range = 50;// Convert.ToInt32(txtRange.Text);

            var generatedFrequencies = new List<Frequency>(to - from);
            var availableRanges = new List<AvailableRange>(to - from + 1);

            //AvailableRange maxAvailableRange = new AvailableRange();

            var random = new Random();

            DateTime first = DateTime.Now;

            for (int i = 0; i < numbers; i++)
            {
                var frequency = new Frequency();

                if (availableRanges.Count == 0)
                {
                    frequency.Number = random.Next(from, to);
                    frequency.Number = (int)Math.Round((decimal)frequency.Number / 5) * 5;
                    int fromValue = frequency.Number - (range / 2);
                    int toValue = frequency.Number + (range / 2);

                    frequency.From = fromValue < from ? from : fromValue;
                    frequency.To = toValue > to ? to : toValue;

                    generatedFrequencies.Add(frequency);

                    var availableRange1 = new AvailableRange { From = @from, To = frequency.From };
                    availableRange1.RangeLong = availableRange1.To - availableRange1.From;
                    availableRanges.Add(availableRange1);

                    var availableRange2 = new AvailableRange { From = frequency.To, To = to };
                    availableRange2.RangeLong = availableRange2.To - availableRange2.From;
                    availableRanges.Add(availableRange2);
                }
                else
                {
                    DateTime d1 = DateTime.Now;
                    int max = 0, index = 0;

                    for (int j = 0; j < availableRanges.Count; j++)
                    {
                        if (availableRanges[j].RangeLong > range)
                        {
                            index = j;
                        }


                    }

                    DateTime d2 = DateTime.Now;
                    double total1 = (d2 - d1).TotalMilliseconds;

                    AvailableRange availableRange = availableRanges[index];

                    if (availableRange.RangeLong <= range)
                    {
                        Console.WriteLine("there is no available pathes " + generatedFrequencies.Count);

                        break;
                    }

                    DateTime d3 = DateTime.Now;
                    double total2 = (d3 - d2).TotalMilliseconds;

                    frequency.Number = random.Next(availableRange.From + (range / 2), availableRange.To - (range / 2));
                    frequency.Number = (int)Math.Round((decimal)frequency.Number / 5) * 5;
                    int fromValue = frequency.Number - (range / 2);
                    int toValue = frequency.Number + (range / 2);

                    frequency.From = fromValue < from ? from : fromValue;
                    frequency.To = toValue > to ? to : toValue;
                    generatedFrequencies.Add(frequency);

                    var availableRange1 = new AvailableRange { From = availableRange.From, To = frequency.From, RangeLong = frequency.From - availableRange.From };
                    availableRanges.Add(availableRange1);

                    //availableRange.From = 1;


                    var availableRange2 = new AvailableRange { From = frequency.To, To = availableRange.To, RangeLong = availableRange.To - frequency.To };
                    availableRanges.Add(availableRange2);

                    //availableRange.To = frequency.From;
                    //availableRange.RangeLong = availableRange.To - availableRange.From;


                    DateTime d4 = DateTime.Now;
                    double total3 = (d4 - d3).TotalMilliseconds;

                    //availableRange.RangeLong = 0;
                    availableRanges.RemoveAt(index);

                    DateTime d5 = DateTime.Now;
                    double total4 = (d5 - d4).TotalMilliseconds;

                    double total = (d5 - d1).TotalMilliseconds;
                    if (total > 1)
                    {
                       // Console.WriteLine(total + @"     :     " + total1 + @"     :     " + total2 + @"     :     " + total3 + @"     :     " + total4 +
                       //   @"    frequency     :     " + frequency.Number + @"  max    :     " + max + @"  index   :     " + index);
                    }
                }

            }
            DateTime last = DateTime.Now;
            Console.WriteLine((last - first));

            Console.ReadLine();
        }
    }


    public class Frequency
    {
        public int From { get; set; }
        public int To { get; set; }
        public int Number { get; set; }
        public override string ToString()
        {
            return " from  :  " + From + " to  :  " + To + " number  :  " + Number;
        }
    }

    public class AvailableRange
    {
        public int From { get; set; }
        public int To { get; set; }
        public int RangeLong { get; set; }

        public override string ToString()
        {
            return " from  :  " + From + " to  :  " + To + " RangeLong  :  " + RangeLong;
        }
    }
}

现在问题是这段代码需要一些时间 当我分析代码时,我希望某些周期需要15毫秒,而其他周期需要0毫秒 如果循环次数很少,则不会出现此问题 我试图解决这个问题,但我找不到答案 有什么问题

3 个答案:

答案 0 :(得分:2)

不使用DateTime进行效果衡量。使用System.Diagnostics.Stopwatch

答案 1 :(得分:1)

您所看到的是System.DateTime结构的16毫秒精度限制。

请参阅http://blogs.msdn.com/b/ericlippert/archive/2010/04/08/precision-and-accuracy-of-datetime.aspx

您需要使用System.Diagnostics.Stopwatch类进行任何类型的性能评估,因为它通常精确到微秒(取决于测试时发生的线程上下文切换次数)。

答案 2 :(得分:0)

我认为您无法获得准确的测量结果,因为这是在多任务处理环境中运行的用户模式代码。即使线程优先级设置为最高,也可以被任何内核模式进程取代。