Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Just curious as to what people think is the best way to calculate how long a search takes and then display time to user.
I am using DateTime with Timespan and also Stopwatch sw = new Stopwatch.
I think using Stopwatch is cleaner code compared to DateTime with timespan.
So I'm just curious as to what professional programmers use.
i.e
Stopwatch WebSearchTime = new Stopwatch();
WebSearchTime.Start();
code to run
WebSearchTime.Stop();
double WST = WebSearchTime.Elapsed.Seconds + (WebSearchTime.Elapsed.Milliseconds / 1000.0);
Thanks
George
Stopwatch is the proper and more accurate way to determine time of execution, than DateTime.Now with DateTime calculation.
Stopwatch Class
The Stopwatch measures elapsed time by counting timer ticks in the
underlying timer mechanism. If the installed hardware and operating
system support a high-resolution performance counter, then the
Stopwatch class uses that counter to measure elapsed time.
Otherwise, the Stopwatch class uses the system timer to measure
elapsed time. Use the Frequency and IsHighResolution fields to
determine the precision and resolution of the Stopwatch timing
implementation.
I know that use of Stopwatch is preferred way but I created code snippets to use DateTime and used to use them:
DateTime start = DateTime.Now;
//logic
System.Diagnostics.Debug.WriteLine("Comment:" + (DateTime.Now - start));
Sure if you need to aggregate results you'll have to create TimeSpan. Now I see whole lot of information being outputted while debugging.
My bad. I measured performance as VARAK suggested and it turned out that DateTime.Now takes about 0.00073ms to evaluate that could be real problem in production if you'll iterate over real big amounts of data so you should use my approach carefully.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Is parallel processing going to be necessary?
Use Stopwatch class to run one algorithm and then run the other one.
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// Code for your algorithm
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Make sure you test your algorithm with numerous size inputs. A sorting algorithm which performs faster than another algorithm on small set may not necessarily perform faster on a larger set.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am building a small program just kind of to apply some of what I've learned and see if I can push my limits to learn more. I have so far created a Character class and used a constructor to create a "character" named Dick. I have also created a number of variables and methods that when called, increment or decrement certain variables and output certain activities that Dick is involved in on the console screen. My question is whether there is a way to track time while the program is running so that I can set the time when the program is started and then keep track of it as it runs so it will adjust variables such as hunger, tiredness, time to go to work, time to leave work, and then when those variables hit certain numbers, they will call the methods such as go to work, eat, go to sleep, leave work. Basically, if I could track time some how, I could use every 5 seconds to update the variables, and then the "game" would basically run itself. Any ideas?
Here is how you could do it using the System.Diagnostics namespace:
Stopwatch time = new Stopwatch(); //Create a new Stopwatch
time.Start(); //Start The Timer
Thread.Sleep(5000); //Sleeps The Program For 5 Seconds
System.WriteLine("The Timer Is At: " + time.Elapsed); //Displays What The Timer Is At, Should Be 5 Seconds.
Once you have started your timer you can ignore the Thread.Sleep(5000); part because that was just to show that the timer counts up to 5 seconds as the program is slept for 5 seconds. After starting the timer you can go back and compare the time.Elapsed() part to check if it is a multiple of 5 and if it is then update your variables, like so:
Stopwatch time = new Stopwatch();
if (time.Elapsed % 5 == 0) { //Checks If The Remainder of The Timer When Divided By 5 Is 0.
//Change Variables Or Do Whatever Here
} else {
//Do Whatever Needs To Be Done If Timer Isn't At An Interval Of 5
}
Hope this was of some help.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
if (!responseDetected)
{
responseDetected = true;
bufferedTonePlay.Stop();
responseStopWatch.Stop(); // to get the response time
TimeTakenOutput.Text = responseStopWatch.Elapsed.Seconds.ToString() + ":" + responseStopWatch.Elapsed.Milliseconds.ToString() +"seconds";
MessageBox.Show("" + TimeTakenOutput.Text); // display the various response time.
//how to continue from here? is it possible to save all this various response time into an array so that i can use them for calculation.
You could save all the entries in a List<TimeSpan>, and then add a new item when your responseStopWatch.Stop() is hit.
responseStopWatch.Stop()
elapsedTimes.Add(TimeSpan.FromTicks(responseStopWatch.Elapsed.Ticks));
When you start the timer again, you need to make sure that it is reset. You can either do a
responseStopWatch.Reset();
responseStopWatch.Start();
Or you can use
responseStopWatch.Restart(); // Stops time interval measurement, resets the elapsed time to zero, and starts measuring elapsed time.
The Stopwatch serves the time in different ways. The easiest way is to handle the ticks (even when storing to database) and then using TimeSpan.FromTicks() when creating a new TimeSpan.
A simple sample:
List<TimeSpan> elapsedTimes = new List<TimeSpan>();
Stopwatch sw = new Stopwatch();
Stopwatch sw2 = new Stopwatch();
sw.Start();
Thread.Sleep(2000); // just to get some time on our stopwatch
sw.Stop();
sw2.Start();
Thread.Sleep(1500); // just to get some time on our stopwatch
sw2.Stop();
// add timespans
TimeSpan span1 = TimeSpan.FromTicks(sw.Elapsed.Ticks);
elapsedTimes.Add(span1);
TimeSpan span2 = TimeSpan.FromTicks(sw2.Elapsed.Ticks);
elapsedTimes.Add(span2);
double seconds = elapsedTimes.Sum(x => x.TotalSeconds);
Then you can do different calculations on the list as you wish. This example contains a quick sum of seconds from each item in the list.
EDIT:
You have supplied very little code in your question, but I'll try to answer your comment.
You would need to have an instance of the List<TimeSpan> which lives throughout the test. When the section (sound) starts, you start the stopwatch. You can then have two reasons to stop the stopwatch. Either user triggered, or timeout triggered. Stop the stopwatch, and log the result with the stop reason. You should couple the timespans with a test id.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I was wondering what is exactly the value of gameTime.ElapsedGameTime.TotalSeconds in a second?
gametime.ElapsedGameTime returns the time elapsed since the last update, not the total game time. For this, you need gametime.TotalGameTime.
It is returned as a TimeSpan, so the last part (TotalSeconds) is a property of that struct. Since it's a TimeSpan, you have full access to other properties, like TotalMilliseconds or methods like Compare.
And since TotalSeconds is a double, it will indeed be 0.5 if the elapsed time is 500 milliseconds.
If the game is running at 60 frames per second the value is likely to be 1 / 60 = 0.0166 seconds.
gameTime.ElapsedGameTime.TotalSeconds, as the name suggests, is the total number of seconds your game has been running. So if your game has been running for 1 second then this will be equal to 1. If your game has been running for a minute then this will be equal to 60.
Is there more to your question?
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Let's suppose I have an interval. Say 15 minutes.
I have a start time of 13:57 and an end time of 15:17.
The time when this process runs happens to be 14:07.
I want the result to be 14:00,14:15,14:30,14:45,15:00,15:15 while retaining the year/month/date, etc.
So far, I have these facts down. The minutes modulo the interval is always zero. I need to count down from the current time until I hit the first mod-zero number which is 14:00.
I then simply increase that number by the interval until I reach my ceiling. My real question is how to come up with an elegant, simple way to find this first floor number.
The interval is a timespan and the other two values are datetimes.
Any ideas?
You can calculate the minute for the first result instance like:
m: current time's minute
new minute part: m - (m % interval)