Page 1 of 1
					
				Efficient code...
				Posted: Sun Aug 01, 2004 7:08 pm
				by Escobar
				Hello everyone!
I have a question about which one of the following two (2) instructions are executed faster by the processor: multiplication (*) or making a comparsion ( >, < or = ) between two values (doesn't matter if it's an integer, float or string or whatever)? Is it faster to make a comparsion between two values than making a multiplication?
Another question: When should you consider to have a function/procedure written "inline" instead of making it a separated code that you make a call for (with a pointer or something)? I'm thinking in terms of speed vs. code size.
Thank you in advance
			 
			
					
				
				Posted: Sun Aug 01, 2004 8:05 pm
				by GPI
				A Multiplikation is slow, very slow (ok, in some cases *2, *4, *8,*16... it is fast). So when you can prevent multiplikations, do it!
			 
			
					
				
				Posted: Sun Aug 01, 2004 8:56 pm
				by tinman
				This code confirms GPI's post (unless I've done something wrong). Certainly in PB anyway. If you're looking to a lower level then you probably also need to take into consider things such as the effect of losing a pipeline due to a branch, branch prediction (instruction caches), speed of executing an instruction, and whether the condition codes are updated as part of a multiplication. This will all be processor specific, with the worst case being pipeline length (meaning you could lose different numbers of instructions) depending on processor.
Code: Select all
start_time.l=GetTickCount_()
For i.l=0 To 1000000000
    If 0>1
    EndIf
Next
end_time.l=GetTickCount_()-start
time.f = end_time - start_time : time = time / 1000
MessageRequester("Took", StrF(time), #PB_MessageRequester_OK)
start_time.l=GetTickCount_()
For i.l=0 To 1000000000
    If 0<1
    EndIf
Next
end_time.l=GetTickCount_()-start
time.f = end_time - start_time : time = time / 1000
MessageRequester("Took", StrF(time), #PB_MessageRequester_OK)
start_time.l=GetTickCount_()
For i.l=0 To 1000000000
    f.l = f * -1
Next
end_time.l=GetTickCount_()-start
time.f = end_time - start_time : time = time / 1000
MessageRequester("Took", StrF(time), #PB_MessageRequester_OK)
Under Windows, the first loop compiles to the following. This shows that the generated code performs an unconditional branch/jump for the if/endif part, which may not actually represent real life if you are comparing against something.
Code: Select all
; For i.l=0 To 1000000000
  MOV    dword [v_i],0
_For1:
  MOV    eax,1000000000
  CMP    eax,dword [v_i]
  JL    _Next2
; If 0>1
  JMP     _EndIf4
; EndIf
_EndIf4:
; Next
_NextContinue2:
  INC    dword [v_i]
  JMP   _For1
_Next2:
 
			 
			
					
				
				Posted: Sun Aug 01, 2004 9:45 pm
				by thefool
				get the asm - optimizing help file.
u just launch it from pb gui then.
 there is a lot there.
all on all its a great read.
and what i have tried so far from it works with PB.
Also it has a great topic about Reducing code size. I know it only
has trics that saves a few bytes, but if u have a really large app and want
to optimize the size, its good to read. Also optimizing speed and such.
but thats just what i suggest
			 
			
					
				
				Posted: Mon Aug 02, 2004 11:06 pm
				by Escobar
				Thank you guys for the answers...
I suspected that multiplications are slower than comparing two values but I wasn't sure. I don't know assembler well but I understod your code 'tinman'  

 thanks! And thank you 'thefool' for your suggestions I will look into it  
 
The reason I'm asking this is that I'm trying to figure out a collision detection routine and I want to get rid of the squareroot calc's and even all the multiplications by substitute it with a comparsion routine/algorithm. Something like Axis Aligned Boundary Boxes.
 
			 
			
					
				
				Posted: Mon Aug 02, 2004 11:55 pm
				by Doobrey
				Escobar wrote:
I suspected that multiplications are slower than comparing two values but I wasn't sure.
A compare just subtracts one number from the other, then checks the status of the condition flags.
 If it`s equal, the zero flag is set, or the carry flag is set if the second number is smaller than the first.
  
 As for how CPU`s do multiplications?? I`ll pass on that, I`ve only just about grasped 68K asm, let alone understood the inner workings!
 As a very bad guess, the simplest way is just to do a looping add, but then you`ve no guarantee of the CPU returning within a set number of cycles...
 
			 
			
					
				
				Posted: Tue Aug 03, 2004 12:37 am
				by freedimension
				Doobrey wrote:
 As for how CPU`s do multiplications?? I`ll pass on that, I`ve only just about grasped 68K asm, let alone understood the inner workings!
 As a very bad guess, the simplest way is just to do a looping add, but then you`ve no guarantee of the CPU returning within a set number of cycles...
No, believe it or not, it's more like you would do with pen and paper 
