Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: Tontow on September 26, 2004, 10:52 AM

Title: best way to find out if x is a even or odd number?
Post by: Tontow on September 26, 2004, 10:52 AM
what is the best way to find out if x is a even or odd number?
Title: Re:best way to find out if x is a even or odd number?
Post by: UserLoser. on September 26, 2004, 11:34 AM

IsOdd = (Number Mod 2)


If if the number is odd, it returns 1, otherwise, 0
Title: Re:best way to find out if x is a even or odd number?
Post by: Banana fanna fo fanna on September 26, 2004, 11:53 AM
Or you could do:


IsEven = Not (Number Mod 2)


;)
Title: Re: best way to find out if x is a even or odd number?
Post by: K on September 26, 2004, 04:33 PM
That seems like a really slow way to do it.  You're doing division and checking the remainder.  Why not


If Number And 1
'   odd.
End If
Title: Re: best way to find out if x is a even or odd number?
Post by: Banana fanna fo fanna on September 26, 2004, 07:09 PM
K just owned us.
Title: Re: best way to find out if x is a even or odd number?
Post by: Grok on September 27, 2004, 12:49 AM
Don't be so sure.  Depends on how you run it.

1 billion iterations:

Option Explicit

Private Sub cmdTest_Click()
   
    Dim T1 As Variant
    Dim T2 As Variant
    Dim lPos As Long, lMax As Long
    Dim Number As Long
    Dim IsOdd As Boolean, IsEven As Boolean
    Dim test As Long
   
    Number = 8391
    test = 2
   
    lMax = 1000000000
    T1 = Now
    Select Case test
    Case 0
        For lPos = 1 To lMax
            IsOdd = (Number Mod 2)
        Next
    Case 1
        For lPos = 1 To lMax
            IsEven = Not (Number Mod 2)
        Next
    Case 2
        For lPos = 1 To lMax
            If Number And 1 Then
                IsOdd = True
            End If
        Next
    End Select
    T2 = Now
    txtTime.Text = Format(DateDiff("s", T1, T2), "0") & " s"
   
End Sub


Interpreted:
Case 0:  41 seconds
Case 1:  45 seconds
Case 2:  63 seconds

Compiled (p-code):
Case 0:  40 seconds
Case 1:  37 seconds
Case 2:  18 seconds

Compiled (machine code):
Case 0:  1 seconds
Case 1:  1 seconds
Case 2:  1 seconds

Need a more accurate timer to figure out speed when compiled to machine code.
Title: Re: best way to find out if x is a even or odd number?
Post by: K on September 27, 2004, 01:40 AM
My premise was that a bitwise and is cheap, while a division operation and checking the remainder is (more) expensive.  Does it really matter if that doesn't apply when running inside the VB IDE?
Title: Re: best way to find out if x is a even or odd number?
Post by: Banana fanna fo fanna on September 27, 2004, 02:09 PM
Quote
Compiled (p-code):
Case 0:  40 seconds
Case 1:  37 seconds
Case 2:  18 seconds

How'd that happen?
Title: Re: best way to find out if x is a even or odd number?
Post by: Grok on September 27, 2004, 03:04 PM
Quote from: K on September 27, 2004, 01:40 AM
My premise was that a bitwise and is cheap, while a division operation and checking the remainder is (more) expensive.  Does it really matter if that doesn't apply when running inside the VB IDE?

It might matter to someone testing 5 billion operations from the IDE :)
Seriously though, no one was complaining about your contribution, which provided a better answer.  I was merely testing the scenarios and posting the results.

$t0rm:  TheMinistered might be able to answer that.
Title: Re: best way to find out if x is a even or odd number?
Post by: K on September 27, 2004, 08:24 PM
What I want to know is how
Not (Value Mod 2)
is faster than
(Value Mod 2).
in P-Code.
Title: Re: best way to find out if x is a even or odd number?
Post by: Grok on September 27, 2004, 11:13 PM
Great question.  I do not know.  One possibility is the load time for the p-code environment accounted for the longer time for test 0.  When I ran the second and third tesst, the program was already loaded.
Title: Re: best way to find out if x is a even or odd number?
Post by: Joe[x86] on September 28, 2004, 10:45 AM
Find if its odd using your own method! :)

Asuming that the number is stored in Text1.Text, the Firing button is Command1, and you have a label called label1, use this code.Private Sub Command1_Click()
Start:
Label1.Caption = "Working"
If Val(Text1.Text) > 1 Then
     Text1.Text = Val(Text1.Text - 2)
     Goto Start
Else
     If Text1.Text = "0" Then MsgBox "Even"
     If Text1.Text = "1" Then MsgBox "Odd"
     Goto Finish
End If
Finish:
Label1.Caption = "Idle"
End Sub


Going 2 by 2, this will take insanely long when large numbers are used. I will modify this code to work by 10,000s, 1,000s, 100s 10s, 8s, 6s, 4s, and 2s.
Title: Re: best way to find out if x is a even or odd number?
Post by: K on September 28, 2004, 07:19 PM
That's an...uh...intriguing way to do it.
Title: Re: best way to find out if x is a even or odd number?
Post by: Magickian on September 28, 2004, 10:08 PM
One can also bitshift then test for 0, but this is similar to and'ing and more of a pain (since you need to know the size of your data), just another method to do the same thing.