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.
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(); stopWatch.ElapsedMilliseconds directly and get rid of intermediate TimeSpan conversionsvar 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); something like this: ?
DateTime start = DateTime.UtcNow; //... processing ... DateTime end = DateTime.UtcNow; Syste.Diagnostics.Debug.WriteLine((end - start).ToString()); 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.
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());
.Subtract()