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.
Please post an example of the code you are using to perform the calculation.
– Oralista de Sistemas
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
– Joao Raposo
This happens the first time this method is accessed. It is only incited within the method. And it is giving an absurd result.
– Rafael Souza
What is the value of the variable
long total
at the end of your code?– Oralista de Sistemas
Actually... the return wasn’t 17 milliseconds?
– PauloHDSousa
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.
– Joao Raposo
Long’s current return gave 17059
– Rafael Souza
And what are the individual Elapsedmilliseconds of each?
– Joao Raposo
It is in the above code I passed. 1 - 0137141; 2 - 0153916; 3 - 0008668; 4 - 0013553; 5 - 0015071; 6 - 0005936; 7 - 0000051;
– Rafael Souza
@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
@korbes The results were obtained through the code I placed. The Timespan.Frommilliseconds(Stopwatch#.Elapsedmilliseconds) is to add the long result.
– Rafael Souza
Each
StopWatch
is a separate instance even? No two or more pointing to the same object No? Puts the code that creates them.– Oralista de Sistemas
@Rsouza, put all the code of the function, just taking out the parts of your hours bank calculation.
– korbes
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.
– Rafael Souza
@Rsouza, so Stopwatch# are members of the class? There may be competition in the method call?
– korbes
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?
– Luiz Vieira
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).– Luiz Vieira