Dumb Question...

Blitz3D Forums/Blitz3D Programming/Dumb Question...

Red Ocktober(Posted 2005) [#1]
... is this comparison

IF ClassName$="ACTOR"
do something
END IF

a lot slower than...

IF ClassName%=1
do something
END IF

... or a lil slower, or not any slower at all... i'm using it in a run loop...

thx

--Mike


Shambler(Posted 2005) [#2]
String compare will be anything from 2-10+ times slower depending on what string is held in 'ClassName$' and what you are comparing it to.

They are both pretty fast...probably not worth worrying about unless you are doing it 1000 times or more in the main loop.

I use constants a bit like this so you get the benefit of speed and its easy to read,
const cActor=1 ;actor class

select class

case cActor



John J.(Posted 2005) [#3]
Yes, they are much slower.

Try this:
Const ACTOR = 1

...

If ClassName = ACTOR
  do something
End If



John J.(Posted 2005) [#4]
Oops.. I should have refreshed before replying (didn't know Shambler already answered your question).


Shambler(Posted 2005) [#5]
It's always good to have a second opinion ;)


Red Ocktober(Posted 2005) [#6]
thanks guys... yeah Sham, JJ... i had CONSTants representing the different 'classes'... i'll probably go back to that... just to be safe...

i forget now why i changed it around to use strings... laziness, most likely...


--Mike


jfk EO-11110(Posted 2005) [#7]
In theory strings may even be faster because the are compared byte by byte. If the first byte is already diffrent, further comparing will be skipped, while comparing two 32bit variables may take longer (thank comparing two bytes).

But of course, this is only a theory. As shambler said, as long as you check only a couple of strings per frame, it's not worth the troubles.


Techlord(Posted 2005) [#8]
Try this speed test:
For tests = 1  To 10

	starttime1=MilliSecs()
	For loop =  1 To 100000
	If ClassNameS$="1"
	;do something
	End If
	Next
	stoptime1=MilliSecs()-starttime1
	
	starttime2=MilliSecs()
	For loop =  1 To 100000
	If ClassNameI%=1
	;do something
	End If
	Next
	stoptime2=MilliSecs()-starttime2
	Print "Test #"+tests+" ------"
	Print "ClassNameS="+stoptime1
	Print "ClassNameI="+stoptime2
	Print
	
Next
Strings are slower...period!


Regular K(Posted 2005) [#9]
My results (if its any use):
ClassNameS/ClassNameI
3/1
3/0
3/1
2/1
6/0
4/1


JoshK(Posted 2005) [#10]
String comparison can be very slow. It is actually faster to poke both strings to banks and compare banks, first by size, then byte by byte.


DH(Posted 2005) [#11]
In theory strings may even be faster because the are compared byte by byte. If the first byte is already diffrent, further comparing will be skipped, while comparing two 32bit variables may take longer (thank comparing two bytes).


Actually, the way the CPU does a comparrison is it does the entire 32 bit register through an AND gate (an array of And gates, but none the less). It's simultaneous.

A string is compared as if you were comparing 2 arrays (which is what a string basically is, just a character array). Each element of the array is passed through the AND gate for the comparrison.

So a string should be slower depending on the length. A length of 1 is still going to be slower than a 32bit register since the instruction handler has to move from the first portion of the array (even though it's a length of 1, it still handles it as an array of arbitrary length) into the comparrison register. So there are a few extra instructions to do this where as a 32 bit register can simply be moved without any offsetting.


Red Ocktober(Posted 2005) [#12]
thx again fellas for the additional input...

--Mike


big10p(Posted 2005) [#13]
In theory strings may even be faster because the are compared byte by byte. If the first byte is already diffrent, further comparing will be skipped, while comparing two 32bit variables may take longer (thank comparing two bytes).
Nah, I don't think so. The CPU doesn't have any 8-bit registers (as far as I know) so a comparison between two bytes will be done using 32-bit registers, just like comparing 2 integers.

Of course comparing strings is slower than comparing integers! The CPU has no concept of strings; they're just a sequence of numbers, as far as it's concerned. Therefore, it has to do a comparison for every character in the 2 strings. potentially, the longer the strings, the more comparisons have to be done.

Note: well that's how I understand the situation but I might be wrong in some area. :)