简体   繁体   中英

Using c# System.DateTime like c++ time_t

In C++ I am able to get the current time when my application starts I can use

time_t appStartTime = time(null);

then to find the difference in seconds from when it started I can just do the same thing, then find the difference. It looks like I should be using "System.DateTime" in C# net, but the MSDN is confusing in its explanation.

How can I use System.DateTime to find the difference in time (in seconds) between when my application starts, and the current time?

Use Now property

DateTime startTime = DateTime.Now;

//work

DateTime currentTime = DateTime.Now;

and then just simply calculate the difference.

currentTime - startTime;

If you would like to measure the performance consider using Stopwatch .

    Stopwatch stopWatch = new Stopwatch();
    stopWatch.Start();

    //work

    stopWatch.Stop();

As everyone suggested... But they were a little wrong :-) Use DateTime.UtcNow , because

  • It's faster ( DateTime.Now calls DateTime.UtcNow )
  • It works around change of DST on/off.

OR

As @Shekhar_Pro suggested (yes, he was right!), use the Stopwatch

var sw = Stopwatch.StartNew()
.... your code
sw.Stop();
var ms = sw.ElapsedMilliseconds;

or

var ticks = sw.ElapsedTicks;

Oh... and I was forgetting... What you are doing is probably worthless in certain situation... You know, 2011 processors are multicore (and even 2010 :-) )... If you app is vaguely multithread you are probably better measuring:

Process.GetCurrentProcess().TotalProcessorTime

This include the use of all the cores used by your app... So on a dual core, using both cores, it will "gain" 2 seconds for every "real time" second.

If you are using this for checking performance and time taken to Execute code then you Best bet is to use StopWatch .

otherwise System.DateTime has a Subtract function which can be used to get a TimeSpan object or even a simple - (subtract) operator will do it. Then that TimeSpan object has a property of TotalSeconds which you can use.

Several ways to do this:

  • Use DateTime.Now. Subtracting produces a TimeSpan. Takes 8 bytes of storage, times up to 8000 years, resolution of 1 millisecond but accurate to 1/64 second on most machines.

  • Use Environment.TickCount. Similar to time_t but relative from machine boot time. Takes 4 bytes of storage, times up to 24 days (49 with a cast), resolution and accuracy same as DateTime.

  • Use Stopwatch. Stored on the heap, resolution is machine dependent but almost always well below a microsecond. Accuracy isn't usually good but repeats decently, assume +/- 5%. Best used to measure small intervals for comparison.

  • Use timeGetTime. This requires pinvoke to use this multimedia timer. Similar to Environment.TickCount, you can get 1 msec accuracy by using timeBeginPeriod. This is not cheap since it has system-wide effects. Best avoided.

Keep in mind that process execution is subject to the vagaries of overall operating system load, your program is sharing resources with the other 70-odd processes that are running. Either DateTime or TickCount has plenty of accuracy for that.

DateTime startTime = DateTime.Now;
//some code
TimeSpan difference = DateTime.Now - startTime;
int seconds = difference.TotalSeconds.Truncate();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM