views:

195

answers:

2

I'm trying to use the Timespan class to create a start time and a stop time, get the difference and ultimately dividing and multiplying the result against another number.

The problem is getting into something I can work with.

Any suggestions?

+6  A: 

The TimeSpan class has a number of members which might be able to assist, for instance TotalSeconds, TotalMinutes, TotalHours etc.

If you want to know the number of operations per second, divide the number of operations by the TotalSeconds member of the TimeSpan object.

Michael Shimmins
I'm attempting to use it to move a 3D model between two Vector3 points in 3-space. I need to know the time displacement to do so.The Formula I am using is position = start location + ((time displacement / total time) * (end location - start location))Perhaps Timespan is not the answer?
Bryan Harrington
it might still be - you just need to decide on your unit.If time is measured in Seconds then use TotalSeconds.If you are using Frames as your unit of measure then TimeSpan won't help (short of calculating Frames per Second).
Michael Shimmins
I'm pretty sure I need milliseconds. I've fooling around with this for an hourTimeSpan start = new TimeSpan();//Some time has passed I promise!! =)TimeSpan stop = new TimeSpan();int time1 = start.Milliseconds;int time2 = stop.Milliseconds;int currTime = time2 - time1;Console.WriteLine(currTime + " " + time1.ToString() + " " + time2.ToString());This did not seem to work either, I get 0 for all values.
Bryan Harrington
Sorry bout that, I did format the code before posting, got bunched up =(
Bryan Harrington
@Bryan When you create a TimeSpan with new you get the same as `TimeSpan.Zero`. Then you subtract zero from zero which is, obviously, zero. Your code would work if you took the current time with `DateTime.UtcNow`. However, if this is the use you will be doing, use the `Stopwatch` class.
Martinho Fernandes
@Bryan: Also, TimeSpan.Milliseconds is not the same as TimeSpan.TotalMilliseconds. Say, the timespan is 42.983 seconds. Milliseconds is 983, while TotalMilliseconds is 42983.
Martinho Fernandes
+1: And considering your usage, most physics calculations use meters/second so it's probably easiest to use TotalSeconds.
SnOrfus
+2  A: 

If you're trying to measure code running time, the stopwatch class might be more appropriate.

SnOrfus