Using C # System.DateTime as C ++ time_t

In C ++, I can get the current time when starting my application. I can use

time_t appStartTime = time(null);

then, to find the difference in seconds since its inception, I can just do the same and then find the difference. It seems like I should use "System.DateTime" in C # net, but MSDN is confused in its explanation.

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

+3
source share
5 answers

Use property

DateTime startTime = DateTime.Now;

//work

DateTime currentTime = DateTime.Now;

and then just calculate the difference.

currentTime - startTime;

If you want to measure performance, use Stopwatch .

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

    //work

    stopWatch.Stop();
+6
source

... :-) DateTime.UtcNow,

  • (DateTime.Now DateTime.UtcNow)
  • DST.

@Shekhar_Pro (, !),

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

var ticks = sw.ElapsedTicks;

... ... , , , ... , 2011 ( 2010:-))... , , , :

Process.GetCurrentProcess().TotalProcessorTime

, ... , , "" 2 " ".

+2

, , StopWatch.

System.DateTime Subtract, TimeSpan - (subtract) . TimeSpan TotalSeconds, .

+1

:

  • DateTime.Now. TimeSpan. 8 , 8000 , 1 , 1/64 .

  • Environment.TickCount. time_t, . 4 , 24 (49 ), , , DateTime.

  • . , , . , , +/- 5%. .

  • timeGetTime. pinvoke . Environment.TickCount, 1 , timeBeginPeriod. , . .

, , 70- , . DateTime TickCount .

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

All Articles