Another "unimportant" performance question. Unimportant because mostly code-readability is much more important than a few milliseconds, but interesting anyway. I noticed that there are differences between different DateTime Comparisons.
I checked 3 alternatives:
Dim clock As New System.Diagnostics.Stopwatch
Dim t1, t2, t3 As Long
Dim Date1 As Date = Date.Now.AddSeconds(2), Date2 As Date = Date.Now
Dim isGreaterThan As Boolean
clock.Start()
For i As Int32 = 1 To 1000000000
isGreaterThan = Date1 > Date2
Next
clock.Stop()
t1 = clock.ElapsedMilliseconds
clock.Reset()
clock.Start()
For i As Int32 = 1 To 1000000000
isGreaterThan = Date.Compare(Date1, Date2) > 0
Next
clock.Stop()
t2 = clock.ElapsedMilliseconds
clock.Reset()
clock.Start()
For i As Int32 = 1 To 1000000000
isGreaterThan = Date1.CompareTo(Date2) > 0
Next
clock.Stop()
t3 = clock.ElapsedMilliseconds
The results:
- Date1 > Date2 = 13207/13251/13267/13569/13100 = 13279 ms
- Date.Compare(Date1, Date2) > 0 = 13510/13194/13081/13353/13092 = 13246 ms
- Date1.CompareTo(Date2) > 0 = 11776/11768/11865/11776/11847 = 11806 ms
Normally i choose first method because it is more readable and fail-safer than the others. Method 3 is the fastest. Is this the method that the compiler would choose when i use the operator overloaded method 1? When yes, why are there differences during runtime? Method 2 is the slowest, maybe because it is shared/static?! UPDATE: added some more values. Now Metod 1 and 2 are pretty equal fast.
Perhaps somebody could clarify it with facts. Thanks.