Main Site Documentation

System.DateTime.Now.Ticks help


#1

Hello, i made a robot, and i want to know in how long he runs through a specific distance.

i use :


startTime = System.DateTime.Now.Ticks;
....
ticks = (int)(System.DateTime.Now.Ticks - startTime) >> 1;
microSeconds = ticks / TicksPerMicrosecond;


it is the best solution ?
and i want to know what is the : “>>1” ? in the line :

ticks = (int)(System.DateTime.Now.Ticks - startTime) >> 1;

thx


#2

1 is a bit shift of one bit. It is a divide by two.

I think the code is attempting to convert the 100 nanosecond ticks into microseconds, but the code would be wrong since the shift of one bit is a binary divide not a decimal divide.


#3

i forgot this :


const int TicksPerMicrosecond = (int)TimeSpan.TicksPerMillisecond / 1000;

and so, if i want the real time value, i have to delete the “>>1” ?


#4

@ Tirmit - You can look at one of several “stopwatch” entries in Codeshare. I’ve got a barebones one there.


#5

thanks ! it works very well