Precise timing

  • Hi

    Anyone know of a way of timing procs, functions or a piece of code that runs in under 1 millisecond??

    Is it possible?

    More out of interest than serious requirement,

    Thanks

  • If a piece of code actually runs in under a millisecond, why would you care to time it?

    I think SQL Server only support timings to within 3 milliseconds.

    Gregory Larsen, DBA

    If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples

    Gregory A. Larsen, MVP

  • Well like I said, was more out of interest that a real need.

    But if you had a piece of code that could be written in two different ways, and both those ways ran in under a millisecond, it might be interesting to see which actually ran faster (given that SQL can do a lot of things in a millisecond), especially if it was to be used multiple times.

    With reference to your second comment, comparing getdate() (using datediff(ms,...) ) both before and after a piece of code would seem to suggest that you can get timings down to 1 millisecond, is this not the case?

    Thanks

    David

  • If you cannot get the small time increments that you are looking for by simply using datepart(ms, @timer, getdate()) then why not loop around your code say 100 times.

    This will magnify the time taken for the code to process. Once you run your alternative code within a loop 100 times you will have the comparison you are looking for.

  • To verify the time to 3 milliseconds try the following code:

    declare @datetime datetime

    set @datetime = '2002-12-16 23:59:59:993'

    print convert(char(17),@datetime,114)

    set @datetime = '2002-12-16 23:59:59:994'

    print convert(char(17),@datetime,114)

    set @datetime = '2002-12-16 23:59:59:995'

    print convert(char(17),@datetime,114)

    set @datetime = '2002-12-16 23:59:59:996'

    print convert(char(17),@datetime,114)

    set @datetime = '2002-12-16 23:59:59:997'

    print convert(char(17),@datetime,114)

    set @datetime = '2002-12-16 23:59:59:998'

    print convert(char(17),@datetime,114)

    set @datetime = '2002-12-16 23:59:59:999'

    print convert(char(17),@datetime,114)

    set @datetime = '2002-12-17 00:00:00.000'

    print convert(char(17),@datetime,114)

    set @datetime = '2002-12-17 00:00:00:001'

    print convert(char(17),@datetime,114)

    set @datetime = '2002-12-17 00:00:00:002'

    print convert(char(17),@datetime,114)

    set @datetime = '2002-12-17 00:00:00:003'

    print convert(char(17),@datetime,114)

    set @datetime = '2002-12-17 00:00:00:004'

    print convert(char(17),@datetime,114)

    set @datetime = '2002-12-17 00:00:00:005'

    print convert(char(17),@datetime,114)

    set @datetime = '2002-12-17 00:00:00:007'

    print convert(char(17),@datetime,114)

    set @datetime = '2002-12-17 00:00:00:009'

    print convert(char(17),@datetime,114)

    set @datetime = '2002-12-17 00:00:00:009'

    print convert(char(17),@datetime,114)

    set @datetime = '2002-12-17 00:00:00:010'

    print convert(char(17),@datetime,114)

    set @datetime = '2002-12-17 00:00:00:011'

    print convert(char(17),@datetime,114)

    set @datetime = '2002-12-17 00:00:00:012'

    print convert(char(17),@datetime,114)

    As far as timing to determine which code is faster. Try running each set of code a million time, and compare how long it takes.

    This should be measurable.

    Gregory Larsen, DBA

    If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples

    Gregory A. Larsen, MVP

  • Consider it verified!

    In which case, can I revise my original post to "Can you time something that runs in less that three millisecond"...no never mind!!, Thanks for your replies

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply