Sign in to follow this  
Followers 0
Manus-Magnus

Compaisons: Int Numbers An Strings, The Fastest ?

6 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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]

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

> 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]

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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]

Share this post


Link to post
Share on other sites

peace. Tx you guy i tested, you test saying > or < are the fastest logicial if it's bit calculated, and so, tx you so much.

simply the idea of USE timer start/stop to test and optimize some points is the final answer i think.

mm

Share this post


Link to post
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
Sign in to follow this  
Followers 0