Jump to content

Compaisons: Int Numbers An Strings, The Fastest ?


Recommended Posts

hello,

Basicly it's optimisation support request... Or general C programming question...

I have some function burning my CPU comparing variables with constant numbers.

have have multiples ways to exit a loop but...

What is the order from the fastest to the slowest compares with (i need order with all not the 1 fastest in this list):

If $x <> [int number] then....

If $x < [int number] then....

If $x > [int number] then....

If $x == [int number] then....

If $x >= [int number] then....

If $x <= [int number] then....

The next question is: same thing but with strings.

Edited by Manus-Magnus
Link to comment
Share on other sites

Seems like ">" is the fastest

$start = TimerStart()

For $i = 0 To 262144
    if $i == 262144 then $x=0
Next

$equal = TimerStop($start) / 1000


$start = TimerStart()

For $i = 0 To 262144
    if $i >= 262144 then $x=0
Next

$ge = TimerStop($start) / 1000


$start = TimerStart()

For $i = 0 To 262144
    if $i <= 262144 then $x=0
Next

$le = TimerStop($start) / 1000


$start = TimerStart()

For $i = 0 To 262144
    if $i NOT = 262144 then $x=0
Next

$ne = TimerStop($start) / 1000


$start = TimerStart()

For $i = 0 To 262144
    if $i < 262144 then $x=0
Next

$less = TimerStop($start) / 1000


$start = TimerStart()

For $i = 0 To 262144
    if $i > 262144 then $x=0
Next

$greater = TimerStop($start) / 1000

MsgBox(4096, "Execution time:", "==" & $equal & @LF & ">=" & $ge & @LF & "<=" & $le & @LF & "NOT=" & $ne & @LF & "<" & $less & @LF & ">" & $greater)
Edited by MattNis

[quote]I was busy waiting all night for the Columbus Day Bunny to come down my chimney and light fireworks in my pumpkin.There's so much wrong with that.Oh, I'm sorry, i forgot you were Jewish.[/quote]

Link to comment
Share on other sites

There are several things wrong with that test. First, == forces a string comparison, this adds tremendous overhead since it's doing a string compare instead of double compare.

Secondly, the Not = test is done incorrectly. Not doesn't work like that Try this to prove it:

If 1 Not = 2 Then MsgBox(4096, "", "Yay")

The correct way to use Not is:

If Not(1 = 2) Then MsgBox(4096, "", "Yay")

The rest of the tests are unbalanced as follows:

> - No assignment is performed, ever

>= - One assignment is performed

< - Assignment is performed every pass but the last pass

<= - Assignment is performed every pass

Running the tests to minimize the amount of asignments occurring, =, >=, <=, <, > all run in the 13 - 14 second range. Not() ran at over 19, but it assigned $x every time but one so it's result is invalid in relation to the others. >= and <= both assigned only once each. (Note: It's impossible to use this particular test to compare both = and Not because one is always going to perform assignment all but one time while the other will only assign once.)

Edited by Valik
Link to comment
Share on other sites

> and >= are still the quicker of all....though not by much

and the times I got changed a negligeable amount from the first test I posted

$start = TimerStart()

For $i = 0 To 262144
    if $i = $i-1 then $x=0
Next

$equal = TimerStop($start) / 1000


$start = TimerStart()

For $i = 0 To 262144
    if $i >= $i+1 then $x=0
Next

$ge = TimerStop($start) / 1000


$start = TimerStart()

For $i = 0 To 262144
    if $i <= $i-1 then $x=0
Next

$le = TimerStop($start) / 1000


$start = TimerStart()

For $i = 0 To 262144
    if NOT($i = $i) then $x=0
Next

$ne = TimerStop($start) / 1000


$start = TimerStart()

For $i = 0 To 262144
    if $i < $i-1 then $x=0
Next

$less = TimerStop($start) / 1000


$start = TimerStart()

For $i = 0 To 262144
    if $i > $i+1 then $x=0
Next

$greater = TimerStop($start) / 1000

MsgBox(4096, "Execution time:", "=" & $equal & @LF & ">=" & $ge & @LF & "<=" & $le & @LF & "!=" & $ne & @LF & "<" & $less & @LF & ">" & $greater)
Edited by MattNis

[quote]I was busy waiting all night for the Columbus Day Bunny to come down my chimney and light fireworks in my pumpkin.There's so much wrong with that.Oh, I'm sorry, i forgot you were Jewish.[/quote]

Link to comment
Share on other sites

if 1 NOT = 2 then MsgBox(0, "","1 Wrong")

if NOT(1 = 2) then MsgBox(0, "","2 Wrong")

you were right about the above

Edited by MattNis

[quote]I was busy waiting all night for the Columbus Day Bunny to come down my chimney and light fireworks in my pumpkin.There's so much wrong with that.Oh, I'm sorry, i forgot you were Jewish.[/quote]

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...