Results 1 to 4 of 4

Thread: Unexpected Results - Integer v. Double

  1. #1

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  2. #2

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Unexpected Results - Integer v. Double

    I fooled with this a lot yesterday, up until I had to watch the Missouri Tigers. I debated whether or not to post this because I was convinced I had missed something. If anyone wants a little more detail about how I conduct these test I will provide it.

    One thing Jon Skeet and I disagree on is what he calls HoistLength, at least we do if my conversion of the code he provided is correct. So, based on his reputation, I must have missed something there.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Unexpected Results - Integer v. Double

    It shouldn't have been faster, but I have found this before.

    The first thing to ask, though, is whether or not you have the checks for integer overflow disabled. That check greatly impacts the performance of integer math. Still, even with the checks off, integer math in .NET is not as fast as it should be.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Unexpected Results - Integer v. Double

    With the checked removed it is better.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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
  •  



Click Here to Expand Forum to Full Width