|
-
Mar 29th, 2009, 08:56 AM
#1
Unexpected Results - Integer v. Double
I can never pass up a chance to test my code timer, so when I came across Jon Skeet's Blog about using For or For Each I couldn't resist. My results appear to be similar to his, but why is Double faster than Integer?
http://msmvps.com/blogs/jon_skeet/default.aspx
Code:
03/29/09 08:47:15.847 CPU - 1.794GHz. Stopwatch(HR) Freq. is 3,579,545
x86 Family 6 Model 13 Stepping 6 Address width - 32 Memory - 2.0GB
One - For Each
Two - by Count
Using Doubles
Number of Test(NT) = 7 Iterations Per NT(IL) = 262,144
Times given in ticks. 1 ms. = 10,000 ticks; 1,000 ms. = 1 second.
Ticks / Loop Fastest? One
0.0193 0.4078 0.4348 -0.0270
NT Base IL One IL Two IL Difference (One - Two)
1 5,056 106,621 113,829 -7,208
2 5,056 107,220 114,569 -7,349
3 5,058 106,818 113,727 -6,909
4 5,089 107,120 114,493 -7,373
5 5,056 106,421 113,785 -7,364
6 5,056 106,983 113,752 -6,769
7 5,058 107,225 113,740 -6,515
Average
5,061 106,915 113,985 -7,070 (-0.707 ms./IL)
03/29/09 08:47:16.328 RunMode = CTRL-F5 End Dewayne Relative Speed Test
Source Code Follows
'
Const itms As Integer = 50
Dim TestData() As Double
Dim TestList As New List(Of Double)
'
Private Function TestCase1() As Boolean 'One
'Test One Code Follows
Dim sum As Double = 0
For Each d As Double In TestData
sum += d
Next
'End Test One Code
Return True
End Function
'
Private Function TestCase2() As Boolean 'Two
'Test Two Code Follows
Dim sum As Double = 0
For i As Integer = 0 To itms - 1
sum += TestData(i)
Next
'End Test Two Code
Return True
End Function
'
Private Sub _init()
'Code needed to get the Test to run. NOT part of the timing.
'Only called once!!!
TestList.Clear()
For x As Integer = 1 To itms
TestList.Add(Short.MaxValue) ' + 0.01)
Next
TestData = TestList.ToArray
End Sub
Code:
03/29/09 08:49:51.550 CPU - 1.794GHz. Stopwatch(HR) Freq. is 3,579,545
x86 Family 6 Model 13 Stepping 6 Address width - 32 Memory - 2.0GB
One - For Each
Two - by Count
Using Integers
Number of Test(NT) = 7 Iterations Per NT(IL) = 262,144
Times given in ticks. 1 ms. = 10,000 ticks; 1,000 ms. = 1 second.
Ticks / Loop Fastest? One
0.0194 0.5785 0.6057 -0.0272
NT Base IL One IL Two IL Difference (One - Two)
1 5,054 151,674 157,853 -6,179
2 5,059 151,893 157,718 -5,825
3 5,055 151,448 158,327 -6,879
4 5,058 151,403 157,754 -6,351
5 5,057 151,319 159,712 -8,393
6 5,057 152,412 162,178 -9,766
7 5,197 151,321 157,835 -6,514
Average
5,076 151,638 158,768 -7,130 (-0.713 ms./IL)
03/29/09 08:49:52.232 RunMode = CTRL-F5 End Dewayne Relative Speed Test
Source Code Follows
'
Const itms As Integer = 50
Dim TestData() As Integer
Dim TestList As New List(Of Integer)
'
Private Function TestCase1() As Boolean 'One
'Test One Code Follows
Dim sum As Integer = 0
For Each d As Integer In TestData
sum += d
Next
'End Test One Code
Return True
End Function
'
Private Function TestCase2() As Boolean 'Two
'Test Two Code Follows
Dim sum As Integer = 0
For i As Integer = 0 To itms - 1
sum += TestData(i)
Next
'End Test Two Code
Return True
End Function
'
Private Sub _init()
'Code needed to get the Test to run. NOT part of the timing.
'Only called once!!!
TestList.Clear()
For x As Integer = 1 To itms
TestList.Add(Short.MaxValue) ' + 0.01)
Next
TestData = TestList.ToArray
End Sub
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|