计算循环完成所需的时间

时间:2017-06-30 13:45:08

标签: c# performance loops for-loop

所以我有以下循环:

for (int i = 1; i < numRows + 2; i++) //numRows was +4, now +2
{

  Console.Clear();

  Console.WriteLine("Number of rows: " + numRows);
  Console.Write("Checking Row #: " + currRowNumber);

  //We want to skip every row that is null and continue looping until we have more than 3 rows in a row that are null, then break
  if (i > 1) {
    i -= 1;
  }

  //Create Worksheet Range
  Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range) excelWorkbookWorksheet.Cells[i, 2];
  string cellValue = Convert.ToString(range.Value);

  if (nullCounter == 3) //was 5
  {
    Console.WriteLine("\nNull row detected...breaking");
    Console.WriteLine("Number of rows deleted: " + numRowsDeleted);
    break;
  }

  if (cellValue != null) {
    if (cellValue.Contains(searchText)) {
      //Console.WriteLine("Deleting Row: " + Convert.ToString(cellValue));
      ((Range) excelWorkbookWorksheet.Rows[i]).Delete(XlDeleteShiftDirection.xlShiftUp);
      numRowsDeleted++;
      //Console.WriteLine("Number of rows deleted: " + numRowsDeleted);
      nullCounter = 0;
      i--;
      currRowNumber++;
      rowsPerSecond = i;
    } else {
      currRowNumber++;
      nullCounter = 0;
    }
  } else {
    nullCounter++;
    //Console.WriteLine("NullCounter: " + nullCounter);
  }
  i++;

}

我想计算每秒循环的行数,然后根据该数字计算完成整个循环所需的时间,具体取决于有多少行。

3 个答案:

答案 0 :(得分:0)

检查在循环开始时设置Stopwatch并在结尾处检查其经过的属性。

答案 1 :(得分:0)

您有两种可能的解决方案:

  1. 选择秒表和计数器。在开始循环之前启动sw并使用ervery循环增加计数器。在循环的walktrough结束时,您可以将counter / sw.Elapsed

  2. 分开
  3. 使用间隔为1000毫秒的计数器和计时器。每次通过For循环时增加计数器。每个刻度线都可以获得当前的循环次数。

  4. 编辑:完成for循环后,停止Timer和StopWatch

答案 2 :(得分:0)

让一些简单的东西运行起来非常简单。考虑以下课程:

public class TimePredictor
{
    private readonly Stopwatch watch = new Stopwatch();
    private double currentProgressRate;

    public void Start() => watch.Restart();
    public void Stop() => watch.Stop();
    public double ElapsedTime => watch.ElapsedMilliseconds;

    public void Update(long currentProgress)
    {
        currentProgressRate = watch.ElapsedMilliseconds / (double)currentProgress;
    }

    public double GetExpectedTotalTime(long total)
        => total * currentProgressRate;

    public double GetExpectedTimeLeft(long total)
        => GetExpectedTotalTime(total) - watch.ElapsedMilliseconds;
}

一个简单的用例:

var repetitions = 200;
var predictor = new TimePredictor();

predictor.Start();

for (int i = 0; i < repetitions; i++)
{
    Thread.Sleep((new Random()).Next(100, 250));

    if (i % 5 == 0)
    {
        predictor.Update(i);
        Console.WriteLine($"Iteration #{i}:");
        Console.WriteLine($"\tExpected total time: {predictor.GetExpectedTotalTime(repetitions) / 1000.0:N1}");
        Console.WriteLine($"\tExpected time left: {predictor.GetExpectedTimeLeft(repetitions) / 1000.0:N1}");
        Console.WriteLine();
    }
}

predictor.Stop();
Console.WriteLine($"Total time: {predictor.ElapsedTime / 1000.0:N1}");