Execution calculation error

2

I have the following question.

I have these following times obtained through Stopwatch , where it was placed inside a method to return the bank balance of the employee's hours. But it's showing me a totally contradictory time with what was calculated by Stopwatch .

Within the method was divided into 7 parts to see where would be the reason to take so much time.

Result:
Data that was returned from the time bank by method: -82:52:24;

  

1 - Time to get 1st marking: 00: 00: 00.0137141;
  2 - Calculation time of the balance: 00: 00: 00.0153916;
  3 - Time to get all the markings: 00: 00: 00.0008668;
  4 - Time to format date / time: 00: 00: 00.0013553;
  5 - Time to sect Information: 00: 00: 00.0015071;
  6 - Time to feed balance: 00: 00: 00.0005936;
  7 - Time to put everything together and show the result: 00: 00: 00.0000051;
  8 - Total Method Time: 00: 00: 17.3199664

I wonder how it is possible for the result from 1 to 7 to take these 17 secs you were informed by the calculation of the sum of all time.

Ex:

private string CalculaSaldo(FUNCIONARIOS funcionario, DateTime ultimoDia, TimeSpan cargaHoraria, TimeSpan tempoAlmoco, int diaTrabalho)
{
  #region 1
      Stopwatch1.Start();
      //CÓDIGO ...
      Stopwatch1.Stop();
  #endregion

  #region 2
     Stopwatch2.Start();
     //CÓDIGO ...
     Stopwatch2.Stop();
  #endregion

  #region 3
     Stopwatch3.Start();
     //CÓDIGO ...
     Stopwatch3.Stop();
   #endregion

   #region 4
     Stopwatch4.Start();
     //CÓDIGO ...
     Stopwatch4.Stop();
   #endregion

   #region 5
     Stopwatch5.Start();
     //CÓDIGO ...
     Stopwatch5.Stop();
   #endregion

   #region 6
     Stopwatch6.Start();
     //CÓDIGO ...
     Stopwatch6.Stop();
   #endregion

   #region 7
     Stopwatch7.Start();
     //CÓDIGO ...
     Stopwatch7.Stop();
   #endregion

   long total = Stopwatch1.ElapsedMilliseconds + Stopwatch2.ElapsedMilliseconds + Stopwatch3.ElapsedMilliseconds + Stopwatch4.ElapsedMilliseconds +
            Stopwatch5.ElapsedMilliseconds + Stopwatch6.ElapsedMilliseconds + Stopwatch7.ElapsedMilliseconds;
   return result + ";tempo total: " + TimeSpan.FromMilliseconds(total).ToString();
}

All points the method was separated with a Stopwatch.

Obg

    
asked by anonymous 14.02.2014 / 13:32

2 answers

1

You are not resetting Stopwatch just by stopping and starting again and the last time will be with the sum of all others. Ex: the second will get the sum of the first and the time itself

    
14.02.2014 / 14:00
0

You can do it this way.

private string CalculaSaldo(FUNCIONARIOS funcionario, DateTime ultimoDia, TimeSpan cargaHoraria, TimeSpan tempoAlmoco, int diaTrabalho)
{
StopwatchTOTAL.Start();
  #region 1
      Stopwatch1.Start();
      //CÓDIGO ...
      Stopwatch1.Stop();
  #endregion

  #region 2
     Stopwatch2.Start();
     //CÓDIGO ...
     Stopwatch2.Stop();
  #endregion

  #region 3
     Stopwatch3.Start();
     //CÓDIGO ...
     Stopwatch3.Stop();
   #endregion

   #region 4
     Stopwatch4.Start();
     //CÓDIGO ...
     Stopwatch4.Stop();
   #endregion

   #region 5
     Stopwatch5.Start();
     //CÓDIGO ...
     Stopwatch5.Stop();
   #endregion

   #region 6
     Stopwatch6.Start();
     //CÓDIGO ...
     Stopwatch6.Stop();
   #endregion

   #region 7
     Stopwatch7.Start();
     //CÓDIGO ...
     Stopwatch7.Stop();
   #endregion
StopwatchTOTAL.Stop();

   long total = StopwatchTOTAL.ElapsedMilliseconds;
   return result + ";tempo total: " + TimeSpan.FromMilliseconds(total).ToString();
}
    
14.02.2014 / 14:01