Error in calculation of execution

Asked

Viewed 167 times

2

I have the following question.

I have these following times obtained through the Stopwatch, where it was placed within a method to return the employee’s hours bank balance. Only that he is 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 long.

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

1 - Time to catch 1st time: 00:00:00.0137141;
2 - Balance calculation time: 00:00:00.0153916;
3 - Time to pick up all appointments: 00:00:00.0008668;
4 - Time to format date/time:00:00:00.0013553;
5 - Time to set 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 would like to know how it is possible for the result of 1 to 7 to take those 17 sec that was informed by calculating the sum of all times.

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 has been separated with a Stopwatch.

Obg.

  • 2

    Please post an example of the code you are using to perform the calculation.

  • The variable that stores the total time may' not be 00:00:00 at the beginning, but as reported by @Renan, without code is not easy

  • This happens the first time this method is accessed. It is only incited within the method. And it is giving an absurd result.

  • What is the value of the variable long total at the end of your code?

  • Actually... the return wasn’t 17 milliseconds?

  • I tested this code in a console application and it works well, try to debug and see what times Stopwatch’s have even before returning the result.

  • Long’s current return gave 17059

  • And what are the individual Elapsedmilliseconds of each?

  • It is in the above code I passed. 1 - 0137141; 2 - 0153916; 3 - 0008668; 4 - 0013553; 5 - 0015071; 6 - 0005936; 7 - 0000051;

  • @Rsouza, your question is still confused. Times 1 to 7 were also obtained from Timespan.Frommilliseconds(Stopwatch#.Elapsedmilliseconds)? What is the relevance of the function’s actual result to your question?

  • @korbes The results were obtained through the code I placed. The Timespan.Frommilliseconds(Stopwatch#.Elapsedmilliseconds) is to add the long result.

  • Each StopWatch is a separate instance even? No two or more pointing to the same object No? Puts the code that creates them.

  • @Rsouza, put all the code of the function, just taking out the parts of your hours bank calculation.

  • The problem would be in the sum msm of the value, the code in this case is not necessary because I have the time each one takes to be executed. Where there is "//CODE" that is the code of my method, and it is between your time meter that when added it generates a contradictory value. I’ll try to solve it another way if there’s no other way.

  • @Rsouza, so Stopwatch# are members of the class? There may be competition in the method call?

  • I didn’t understand what "Data that was returned from the database by the method" means: where does this value come from? a negative value is correct?

  • Another thing (in relation to your comment in response to Joao): you are sure that these individual values of ElapsedMilliseconds are correct? For example, 137141 milliseconds is 137.141 seconds (about 2 minutes and 17 seconds, or "00:02:17.1410000"). This is not the same thing as "00:00:00.0137141" (13.7141 milliseconds).

Show 12 more comments

2 answers

1

You are not resetting the Stopwatch just stopping and starting again and the last time will get the sum of all the others. Ex: the second will take the sum of the first and his own time

  • This happens the first time this method is accessed. It is only incited within the method. And it is giving an absurd result.

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();
}
  • I also tried this way Paul and gave the same thing ! o0' very strange this result.

  • I imagine, however negligible a difference it may be, StopwatchTOTAL will end up recording the time used by the Start/Stop functions of others.

  • Yes @Conrad, but that would be something derisory that wouldn’t even last 100 milliseconds.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.