Reputation: 6168
How I print in a textbox or at the output the time that the program spend running?
I want it to be displayed into a for-loop, to get how much time each for-loop needs.
Upvotes: 7
Views: 31382
Reputation: 57573
You could try:
DateTime dt = DateTime.Now;
for (.......)
{
}
TimeSpan ts = DateTime.Now - dt;
textbox1.Text = ts.TotalMilliseconds.ToString();
or (according to MSDN) if you need better resolution
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
for (.......)
{
}
stopWatch.Stop();
textbox1.Text = stopWatch.ElapsedMilliseconds.ToString();
Upvotes: 18
Reputation: 62484
var watch = System.Diagnostics.Stopwatch.StartNew();
for()
{
// ..
}
watch.Stop();
// Format 00:00:02.0001008
string elapsed = watch.Elapsed.ToString();
// Milliseconds like 2000 for 2 seconds
string elapsedMs = watch.ElapsedMilliseconds.ToString();
System.Diagnostics.Debug.WriteLine(elapsed);
Upvotes: 16
Reputation: 11916
Add these Two Lines- Between your For loop or between the code where you want to test the functinality speed.
Debug.WriteLine(DateTime.Now.ToLongTimeString());
--your code
Debug.WriteLine(DateTime.Now.ToLongTimeString());
Upvotes: 0
Reputation: 12608
You could store the current DateTime on application startup, then create a timer that fires every second. When it fires you get the current DateTime, subtract them to get the TimeSpan and use that to fill your textbox.
Upvotes: 0
Reputation: 44595
something like this: ?
DateTime start = DateTime.UtcNow;
//... processing ...
DateTime end = DateTime.UtcNow;
Syste.Diagnostics.Debug.WriteLine((end - start).ToString());
Upvotes: 0