0

I tried to get the exact start and stop time of interfaces.GetIPv4Statistics().BytesReceived;.

NetworkInterface interfaces;
public Form1()
{
    InitializeComponent();
    if (NetworkInterface.GetIsNetworkAvailable())
    {
        interfaces = NetworkInterface.GetAllNetworkInterfaces()[0];
    }
    Stopwatch timer = Stopwatch.StartNew();
    var time1 = DateTime.Now.ToString("HH:mm:ss:fff");
    //timer stop function take how much time? how if not ignored?
    timer.Stop();
    TimeSpan timespan = timer.Elapsed;
    //10ms for the conversion
    Console.WriteLine("Convert take time {0:00}:{1:00}:{2:000}", 
        timespan.Minutes, timespan.Seconds, timespan.Milliseconds);

    var timeStartGetStatistic = DateTime.Now.ToString("HH:mm:ss:fff");
    var currentByteReceive = interfaces.GetIPv4Statistics().BytesReceived;
    var timeEndGetStatisticAndConvert = DateTime.Now.ToString("HH:mm:ss:fff"); 

    Console.WriteLine("Start:\t{0}\nBytes Received:\t{1}\nStop:\t{2}",
        timeStartGetStatistic, currentByteReceive, timeEndGetStatisticAndConvert);

I use Stopwatch to get the time needed for DateTime.Now.ToString("HHmmss");

I thought the timeEndGetStatisticAndConvert is the time includes also the time for conversion to string.

but the result is

Convert take time 00:00:010

Start: 23:04:12:134

Bytes Received: 700116647

Stop: 23:04:12:134

The start and stop time is the same in resolution of 1ms!!

So Stopwatch show wrong elapsed timespan?

DateTime.Now.ToString() not function as imagine?

Or when we display the result of DateTime.Now.ToString(), it get the time first then it only convert to string? (obviously this is the answer and sorry for this logical error)

By the way, I verify this

Stopwatch timer = Stopwatch.StartNew();
var currentByteReceive1 = interfaces.GetIPv4Statistics().BytesReceived;
timer.Stop();

is 0ms....

So I wonder in C#, what is the SMALLEST time resolution that can be used to display current time and how to display it?

and finally the exact start and stop time of interfaces.GetIPv4Statistics().BytesReceived; is showed by this

Contradiction Happen Here!!!

The real function start time should be AFTER 10ms I get the first time but not BEFORE!! And then I will have the start time larger than the end time!!!

//variable name change because the real situation
//should be Add POSITIVE timespan.Milliseconds but not Add NEGATIVE
var timeStartGetStatisticAndConvert = DateTime.Now.AddMilliseconds(-(timespan.Milliseconds)).ToString("HH:mm:ss:fff");
var currentByteReceive = interfaces.GetIPv4Statistics().BytesReceived;
var timeEndGetStatistic = DateTime.Now.ToString("HH:mm:ss:fff"); 

Console.WriteLine("Start:\t{0}\nBytes Received:\t{1}\nStop:\t{2}",
    timeStartGetStatisticAndConvert, currentByteReceive, timeEndGetStatistic);

Convert take time 00:00:010

//If change sign, Start: 23:04:14:124

Start: 23:04:12:124

Bytes Received: 700116647

Stop: 23:04:12:134

thanks. I will ask the contradiction part in another post.

V-SHY
  • 3,925
  • 4
  • 31
  • 47
  • Stopwatch is pretty accurate (more accurate than the storing the "Now" value twice alternative). Why you think that the time measurements are wrong? Bear in mind that it can only deal with ticks; anything quicker than one tick (100 nanoseconds; what might happen in quite a few scenarios) would output zero. – varocarbas Mar 04 '14 at 15:48
  • @varocarbas 1 ticks is always equal to 100 nanoseconds or it has relationship with microprocessor speed? thanks. – V-SHY Mar 04 '14 at 16:24
  • 1
    The definition of tick (http://msdn.microsoft.com/en-us/library/system.datetime.ticks(v=vs.110).aspx) I THINK that is constant. But Stopwatch has "particular ticks", which might be related to the CPU. – varocarbas Mar 04 '14 at 16:28
  • 1
    I think my question is very similar to these two posts, http://stackoverflow.com/questions/1416139/how-to-get-timestamp-of-tick-precision-in-net-c?lq=1 and http://stackoverflow.com/questions/8889912/searching-for-smallest-time-value-in-c-sharp . Thanks for your information about tick is the smallest time unit in C#. – V-SHY Mar 04 '14 at 16:32

3 Answers3

0

As far as I know, DateTime.Ticks provides the smallest resolution of time in C#. Beyond that you'll need high resolution timers provided by the Windows API; they are normally used for media players (which require a very high precision timer).

kevin
  • 2,196
  • 1
  • 20
  • 24
  • any hints to implement the DateTime.Ticks to get the exact start and stop time in smallest time resolution of `var currentByteReceive = interfaces.GetIPv4Statistics().BytesReceived;`? thanks – V-SHY Mar 04 '14 at 15:55
0

That looks really complicated. It could be because of the format you are selecting for your DateTime ("HH:mm:ss:fff").

interfaces.GetIPv4Statistics().BytesReceived means `interfaces is already initialized and the data is already there, I believe (i.e. BytesReceived).

Basically, what you want is:

var start = DateTime.Now;
if (NetworkInterface.GetIsNetworkAvailable())
{
    interfaces = NetworkInterface.GetAllNetworkInterfaces()[0];
}
var currentByteReceive = interfaces.GetIPv4Statistics().BytesReceived;
Console.WriteLine("Timespan: {0}", DateTime.Now - start);

If, that is, I understand what you are trying to do.

  • No, I don't want the timespan but I want the exact start and stop time in smallest time resolution of `var currentByteReceive = interfaces.GetIPv4Statistics().BytesReceived;` Anyway, thanks~ – V-SHY Mar 04 '14 at 15:54
  • OK. A span can only resolve down to the nearest millisecond. `(DateTime.Now - start).TotalMilliseconds;` - if that helps. –  Mar 04 '14 at 19:17
0

Try to use Elapsed or Tick propertie

Stopwatch timer = Stopwatch.StartNew();
            var currentByteReceive1 = interfaces.GetIPv4Statistics().BytesReceived;
            timer.Stop();
            Console.WriteLine(timer.Elapsed);

It gives me this 00:00:00.0026059 (2,6 ms.)

Viliam
  • 636
  • 9
  • 17
  • so the Stopwatch can provide smallest 0.1 microsecond result, by the way I want the Start and Stop time but not the duration, thanks anyway~ – V-SHY Mar 04 '14 at 16:08