• Welcome to Valhalla Legends Archive.
 

best way to find out if x is a even or odd number?

Started by Tontow, September 26, 2004, 10:52 AM

Previous topic - Next topic

Tontow

what is the best way to find out if x is a even or odd number?

UserLoser.

#1

IsOdd = (Number Mod 2)


If if the number is odd, it returns 1, otherwise, 0

Banana fanna fo fanna


K

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


Grok

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.

K

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?

Banana fanna fo fanna

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

How'd that happen?

Grok

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.

K

What I want to know is how
Not (Value Mod 2)
is faster than
(Value Mod 2).
in P-Code.

Grok

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.

Joe[x86]

#11
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.
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

K


Magickian

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.