7

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.

2

5 Answers 5

18

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(); 
Sign up to request clarification or add additional context in comments.

4 Comments

MSDN documentation specifically advises against using the DateTime class for benchmarking purposes.
@R0MANARMY: yes, you're right. I edited my post including both solutions. Thanks
You can use stopWatch.ElapsedMilliseconds directly and get rid of intermediate TimeSpan conversions
@Marco : yep but also you've skipped some code for string formatting. They are usign TimeSpan to output formatted date time not only milliseconds, second MSDN example which outputs Milliseconds does not use TimeSpan
16
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); 

1 Comment

+1 for you, because your answer was the first and it's correct!!
0

something like this: ?

DateTime start = DateTime.UtcNow; //... processing ... DateTime end = DateTime.UtcNow; Syste.Diagnostics.Debug.WriteLine((end - start).ToString()); 

2 Comments

MSDN documentation specifically advises against using the DateTime class for benchmarking purposes.
sure I know there are finer timers, this is good if you want to see seconds or minutes... :)
0

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.

2 Comments

Wouldn't that just count the seconds?
You update the input box every second, not measure the timespan.
0

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()); 

3 Comments

This won't be accurate because you'll be measuring not just how long the code took to execute but how long it took to turn a DateTime into a string + run the code. Granted if the code runs in something on the order of minutes a few milliseconds will be insignificant, but for shorter running could it could make a difference.
@Romanarmy , you are correct, if it kind of time critical Nuclear project.. And as you know most of the projects we develop everyday are Nuclear Web Applications.
no, but we've all written code that executes in a tight loop, and making it less efficient means poor user experience because the UI locks up for a second every time a user clicks a button.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.