I have a block of code that has to calculate code executing duration. The method where the block is called every second via DispatcherTimer.
As you can see from photo a span4.totalseconds is about 0, but the (DateTimeOffset.Now - now2) duration is about 1 second.
//some code here
span4 = DateTimeOffset.Now - now2;
someInfo.Text = (DateTimeOffset.Now - now2).TotalSeconds.ToString();
now2 = DateTimeOffset.Now;
The way you measure execution time in .Net is using Stopwatch class:
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Thread.Sleep(10000);
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
because DateTimeOffset.Now will have a diferent value each time you call it... why you are doing the same substraction two times? try that:
span4 = DateTimeOffset.Now - now2;
someInfo.Text = (span4).TotalSeconds.ToString();
now2 = DateTimeOffset.Now;
Related
I'm trying to write something to stop running the code after 15 seconds of running.
I don't want While loop or any kind of loop to be used and would like to use IF-ELSE conditions instead as it would make it easier for me in my code.
The part of code I want to stop being executed after 15 seconds is a FOR loop itself. Let's consider the below code for example:
for (int i = 1; i < 100000; i++)
{
Console.WriteLine("This is test no. "+ i+ "\n");
}
How would you stop this loop after 15 seconds of running?
You can assign DateTime variable before the loop having the current date and time, then in each loop iteration simply check if 15 seconds have passed:
DateTime start = DateTime.Now;
for (int i = 1; i < 100000; i++)
{
if ((DateTime.Now - start).TotalSeconds >= 15)
break;
Console.WriteLine("This is test no. "+ i+ "\n");
}
Update: while the above will usually work, it's not bullet proof and might fail on some edge cases (as Servy pointed out in a comment), causing endless loop. Better practice would be using the Stopwatch class, which is part of System.Diagnostics namespace:
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 1; i < 100000; i++)
{
if (watch.Elapsed.TotalMilliseconds >= 500)
break;
Console.WriteLine("This is test no. " + i + "\n");
}
watch.Stop();
I'm posting my answer from my older post because it's more relevant here,
I think you need to measure time and stop the code after particular time say "15 seconds" ,StopWatch class can help you out.
// Create new stopwatch instance
Stopwatch stopwatch = new Stopwatch();
// start stopwatch
stopwatch.Start();
// Stop the stopwatch
stopwatch.Stop();
// Write result
Console.WriteLine("Time elapsed: {0}",stopwatch.Elapsed);
// you can check for Elapsed property when its greater than 15 seconds
//then stop the code
Elapsed property returns TimeSpan instance you would do something like this.
TimeSpan timeGone = stopwatch.Elapsed;
To fit your scenario you can do something like this
Stopwatch stopwatch = new Stopwatch();
TimeSpan timeGone;
// Use TimeSpan constructor to specify:
// ... Days, hours, minutes, seconds, milliseconds.
// ... The TimeSpan returned has those values.
TimeSpan RequiredTimeLine = new TimeSpan(0, 0, 0, 15, 0);//set to 15 sec
While ( timeGone.Seconds < RequiredTimeLine.Seconds )
{
stopwatch.Start();
Start();
timeGone = stopwatch.Elapsed;
}
Stop();//your method which will stop listening
Some useful links
MSDN StopWatch
Better you can use below code, which would help to improve in performance.
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(2));
I need a method that give me the time elapsed awhile my process. I call it at start the process and call it again at finish the process, and the method print the total time elapsed.
This is my method, but always print the time in 00:00. Why is happening this??
public void GetTimeElapsed(string filePath, int logSelected, bool time, IUserOptions userOptions)
{
var stopwatch = new System.Diagnostics.Stopwatch();
LogBinaryWriter BinaryWriter = new LogBinaryWriter();
string timeElapsed = "";
if(time == true)
{
stopwatch.Start();
}
if (time == false)
{
stopwatch.Stop();
TimeSpan timeSpan = stopwatch.Elapsed;
timeElapsed = (string.Format("\nFile Generated: {0}\nTime Elapsed: {1} minute(s) {2} second(s)",
BinaryWriter.CreateLogFileName(filePath, Convert.ToInt32(logSelected)),
timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10 + "\n"));
userOptions.DisplayUserMessage(timeElapsed);
}
}
Look where you're declaring stopwatch; it's a local variable. That means you're creating and using two different stopwatches; the first one is started when you call the method with a "true" parameter, then disposed of when the method ends and the variable goes out of scope. The second is declared, never started, then its time examined and logged.
To solve the problem, declare an instance variable ("field") for the Stopwatch. That will keep it in scope as long as the object is around, meaning it will keep running after the method ends, and will still be the same instance when you come back to it to stop and examine it.
Your stopwatch variable is local. When you call the function a second time, it's initialized again.
You need to move the declaration up to class level.
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
public void GetTimeElapsed(string filePath, int logSelected, bool time, IUserOptions userOptions)
{
... etc
You're creating a new stopwatch each time this method is called, but it looks like that should be persistent between method calls.
Take the stopwatch variable declaration outside of the method.
What about using:
var startTime = DateTime.Now;
... your code
var elapsed = DateTime.Now - startTime;
if(time == true)
{
stopwatch.Start();
}
if (time == false)
{
stopwatch.Stop();
...
}
If time is true, you only ever start the stopwatch.
If it is false, you never start it.
A better structure would be:
if(time)
{
stopwatch.Start();
}
... //code to measure here
if (time)
{
stopwatch.Stop();
// log elapsed time
}
Note:
If you have a boolean type, you don't compare it to true or false. Just use it directly and if you want to invert it just use !.
You need to use timeSpan.TotalMinutes instead timestamp.Minutes. Refer timespan documentation
C#
I need to show the time running while the process is doing, shows the seconds increasing, normally: 00:00:01, 00:00:02, 00:00:03..... etc.
I'm using this code:
var stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
//here is doing my process
stopwatch.Stop();
when the process stop, I show the time ELAPSED, with this:
TimeSpan ts = stopwatch.Elapsed;
...and this:
{0} minute(s)"+ " {1} second(s)", ts.Minutes, ts.Seconds, ts.Milliseconds/10.
this show the total time elapsed, But I need show the time running in console.
How can I do this?
Try
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
while (true)
{
Console.Write(stopwatch.Elapsed.ToString());
Console.Write('\r');
}
UPDATE
To prevent display of milliseconds:
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
while (true)
{
TimeSpan timeSpan = TimeSpan.FromSeconds(Convert.ToInt32(stopwatch.Elapsed.TotalSeconds));
Console.Write(timeSpan.ToString("c"));
Console.Write('\r');
}
If I understand correctly, you are wanting to continually update the timespan on the console while the work is still proceeding. Is that correct? If so, you will need to either do the work in a separate thread, or update the console in a separate thread. One of the best references I've seen for threading is http://www.albahari.com/threading/.
Hope this helps.
Is there a class in C# that can give me clock ticks, seconds consumed by a method? I guess I have two wrap that functionality around function to time the ticks and seconds taken up.
You could use the System.Diagnostics.Stopwatch class.
Stopwatch sw = new Stopwatch();
sw.Start();
// Your method call here...
sw.Stop();
// Get the elapsed time
TimeSpan elapsed = sw.Elapsed;
From here, you can use the TimeSpan.Ticks or the TimeSpan.TotalSeconds properties to determine the elapsed ticks or elapsed seconds, respectively.
If you wanted to, you could use a method along the following lines to "wrap" that functionality around a function, as you mentioned (just an idea, you'd probably want to tweak this code to suit your specific purposes -- passing in arguments, etc.):
public static T ExecuteWithElapsedTime<T>(Func<T> function, out TimeSpan elapsedTime)
{
T rval;
Stopwatch sw = new Stopwatch();
sw.Start();
rval = function();
sw.Stop();
elapsedTime = sw.Elapsed;
return rval;
}
And you could call it like this (where myFunc is a function that returns an int):
TimeSpan elapsed;
int result = ExecuteWithElapsedTime(myFunc, out elapsed);
Might be simpler though to not even bother with a method like this, and just put the Stopwatch code inline with your method call.
Use:
using System.Diagnostics;
...
var sw = Stopwatch.StartNew();
DoYaThing();
Console.WriteLine("{0} Elapsed", sw.Elapsed);
There's the high resolution timer ...
Also, iirc a TimeSpan can give you a measurement in ticks back.
You can check out the [System.TimeSpan] class and wrap that around your method.
I have seen other user posts which show Stopwatch measuring time spent in "Thread.Sleep(5000)" to be around 5000ms.
But my program produces the following results
for (int i = 0; i < 20; ++i)
{
Stopwatch sw = Stopwatch.StartNew();
DateTime start = DateTime.Now;
Thread.Sleep(5000);
sw.Stop();
Console.Out.WriteLine(
"StopWatch Diff:" +
sw.ElapsedMilliseconds.ToString());
Console.Out.WriteLine(
"DateTime Diff:" +
DateTime.Now.Subtract(start).TotalMilliseconds.ToString());
}
StopWatch Diff:1684
DateTime Diff:5262.592
StopWatch Diff:1625
DateTime Diff:4997.12
StopWatch Diff:1604
DateTime Diff:4997.12
StopWatch Diff:1601
DateTime Diff:4997.12
StopWatch Diff:1690
DateTime Diff:4997.12
StopWatch Diff:1603
Is it just me who is observing this behaviour? Why stopwatch measures 1.6 seconds when 5 seconds have actually passed. It is the time that the thread is actually running?
The Stopwatch class is not reliable.
This is unreliable on processors that do not have a constant clock speed (most processors can reduce the clock speed to conserve energy). This is explained in detail here.