开发者

how to get time to the extent of exact millisecond in vb.net

I am developing an application that is very very time critical. I want to put a log writer in the code so that i can track the events and find out where the application is wasting the time.But the problem is that vb.net tells the time only at a interval of 15.25 milliseconds what to do. although msdn documentation says that we can get the time in millisecond. To understand my problem better pls go through the following code. this code is written on a click event of a 开发者_运维知识库button and it writes down the time in a log file. the problem is that it writes time like 16,16,16 and after some time when the dx value reaches to say 5000 it jumps to 31 and then to 46 and so on. Can anyone explain.

    dx = 1
    While dx <= 100000
        dx = dx + 1
        SRLog1.WriteLine("dx vALUE > " & CStr(dx) & " Ticks -> " + CStr(Now.Millisecond))
    End While


You can create an incremental clock with much higher resolution by using the Stopwatch class. For example:

    Dim sw = Stopwatch.StartNew()
    Dim start = sw.Elapsed
    '' Do stuff
    '' ...
    Dim sample = sw.Elapsed
    Console.WriteLine(sample - start)

This is still not likely to work out that well for the code in your snippet, incrementing an integer takes less than half a nanosecond. You'll actually measure how long it takes to log, that ought to be slow enough to get different output values.


If you want to find out which parts of your application are waisting time, then you should try using a profiler. Two possible products would be JetBrains dotTrace and RedGates ANTS. You should also take a look at this question here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜