Jump to content



1

Which if-then is faster?


15 replies to this topic

#1 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • 20,911 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Mon Oct 27, 2008 2:44 AM

Here are two similar if-then statements:

  if a=255 && b>c then goto moosenipple

  if a=255 then if b>c then goto moosenipple

Is one faster than the other or do they both take the same amount of time? Seems like the second one would be faster, but I don't know what happens to them after they are compiled.


Thanks.

#2 yuppicide OFFLINE  

yuppicide

    I am the Black Knight. Give me your money!

  • 6,933 posts
  • Location:New Jersey

Posted Mon Oct 27, 2008 9:06 AM

I've never used the second one, and actually I never even knew it was possible!

View PostRandom Terrain, on Mon Oct 27, 2008 4:44 AM, said:

Here are two similar if-then statements:

  if a=255 && b>c then goto moosenipple

  if a=255 then if b>c then goto moosenipple

Is one faster than the other or do they both take the same amount of time? Seems like the second one would be faster, but I don't know what happens to them after they are compiled.


Thanks.


#3 Nukey Shay OFFLINE  

Nukey Shay

    Sheik Yerbouti

  • 20,458 posts
  • Location:The land of Gorch

Posted Mon Oct 27, 2008 9:53 AM

They should both take the same amount of time from what I can tell.

	LDA A
	CMP #$FF
	BNE nojump
	LDA B
	CMP C
	BCC nojump
	JMP moosenipple
nojump:

Right?

Edited by Nukey Shay, Mon Oct 27, 2008 9:54 AM.


#4 SeaGtGruff OFFLINE  

SeaGtGruff

    River Patroller

  • 4,543 posts
  • Location:Georgia, USA

Posted Mon Oct 27, 2008 10:55 AM

View PostNukey Shay, on Mon Oct 27, 2008 10:53 AM, said:

They should both take the same amount of time from what I can tell.

	LDA A
	CMP #$FF
	BNE nojump
	LDA B
	CMP C
	BCC nojump
	JMP moosenipple
nojump:

Right?
I can't compile a test right now, but I believe you're right-- they should compile to essentially the same code, although there will be some differences in the jump labels that bB creates when it's compiling them.

Michael

#5 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • 20,911 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Mon Oct 27, 2008 4:03 PM

View PostSeaGtGruff, on Mon Oct 27, 2008 11:55 AM, said:

I can't compile a test right now, but I believe you're right-- they should compile to essentially the same code, although there will be some differences in the jump labels that bB creates when it's compiling them.
If you find out they're the same, that will be good to know. I could use either one and won't have to worry about which is faster.

While we're on the subject of speed, do you know if a for-next loop is faster than a fake for-next loop?

Here's a real for-next loop:
  for x = 1 to 20
  [Mind-blowing code goes here.]
  next

Here's a fake for-next loop:
  temp5 = 0 : temp6 = 20
fingernailpudding
  temp5 = temp5 + 1
  [Mind-blowing code goes here.]
  if temp5<temp6 then goto fingernailpudding

I hope the fake for-next loop isn't slower since it's useful if the code in the middle needs to jump out of there if a certain condition is met.


Thanks.

#6 SeaGtGruff OFFLINE  

SeaGtGruff

    River Patroller

  • 4,543 posts
  • Location:Georgia, USA

Posted Mon Oct 27, 2008 4:09 PM

View PostRandom Terrain, on Mon Oct 27, 2008 5:03 PM, said:

View PostSeaGtGruff, on Mon Oct 27, 2008 11:55 AM, said:

I can't compile a test right now, but I believe you're right-- they should compile to essentially the same code, although there will be some differences in the jump labels that bB creates when it's compiling them.
If you find out they're the same, that will be good to know. I could use either one and won't have to worry about which is faster.

While we're on the subject of speed, do you know if a for-next loop is faster than a fake for-next loop?

Here's a real for-next loop:
  for x = 1 to 20
  [Mind-blowing code goes here.]
  next

Here's a fake for-next loop:
  temp5 = 0 : temp6 = 20
fingernailpudding
  temp5 = temp5 + 1
  [Mind-blowing code goes here.]
  if temp5<temp6 then goto fingernailpudding

I hope the fake for-next loop isn't slower since it's useful if the code in the middle needs to jump out of there if a certain condition is met.


Thanks.
I'll check them out tonight. :) But first I absolutely must watch Heroes. (That's right. I don't care if the rest of the people in the world supposedly think that Heroes sucks these days, there are so very few TV shows that I care to watch anymore, and Heroes is definitely one of them.) :D

Michael

#7 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • 20,911 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Mon Oct 27, 2008 5:29 PM

View PostSeaGtGruff, on Mon Oct 27, 2008 5:09 PM, said:

I'll check them out tonight. :) But first I absolutely must watch Heroes. (That's right. I don't care if the rest of the people in the world supposedly think that Heroes sucks these days, there are so very few TV shows that I care to watch anymore, and Heroes is definitely one of them.) :D
It's in my list of favorite shows:

http://www.randomter...ies-tv-f-m.html

Those pages need to be updated. I have a few more shows I need to add to the list.

#8 SeaGtGruff OFFLINE  

SeaGtGruff

    River Patroller

  • 4,543 posts
  • Location:Georgia, USA

Posted Mon Oct 27, 2008 11:38 PM

The two ifs you posted compile exactly the same (except for the labels that bB creates):

   1515  f48f				   .L01 	;  if a  =  255  &&  b  >  c then goto moosenipple
   1516  f48f
   1517  f48f			   a5 d6			  LDA	a
   1518  f491			   c9 ff			  CMP	#255
   1519  f493			   d0 09			  BNE	.skipL01
   1520  f495				   .condpart0
   1521  f495			   a5 d8			  LDA	c
   1522  f497			   c5 d7			  CMP	b
   1523  f499			   b0 03			  BCS	.skip0then
   1524  f49b				   .condpart1
   1525  f49b			   4c bf f4 		  jmp	.moosenipple
   1526  f49e
   1527  f49e				   .skip0then
   1528  f49e				   .skipL01
   1529  f49e				   .
   1530  f49e						;
   1531  f49e
   1532  f49e				   .L02 	;  if a  =  255 then if b  >  c then goto moosenipple
   1533  f49e
   1534  f49e			   a5 d6			  LDA	a
   1535  f4a0			   c9 ff			  CMP	#255
   1536  f4a2			   d0 09			  BNE	.skipL02
   1537  f4a4				   .condpart2
   1538  f4a4			   a5 d8			  LDA	c
   1539  f4a6			   c5 d7			  CMP	b
   1540  f4a8			   b0 03			  BCS	.skip2then
   1541  f4aa				   .condpart3
   1542  f4aa			   4c bf f4 		  jmp	.moosenipple
   1543  f4ad
   1544  f4ad				   .skip2then
   1545  f4ad				   .skipL02
However, you can save a few bytes and a few cycles if you leave off the goto-- as long as moosenipple isn't too far away:

   1515  f48f				   .L01 	;  if a  =  255  &&  b  >  c then moosenipple
   1516  f48f
   1517  f48f			   a5 d6			  LDA	a
   1518  f491			   c9 ff			  CMP	#255
   1519  f493			   d0 06			  BNE	.skipL01
   1520  f495				   .condpart0
   1521  f495			   a5 d8			  LDA	c
   1522  f497			   c5 d7			  CMP	b
   1523  f499			   90 18			  bcc	.moosenipple
   1524  f49b				   .skipL01
   1525  f49b				   .
   1526  f49b						;
   1527  f49b
   1528  f49b				   .L02 	;  if a  =  255 then if b  >  c then moosenipple
   1529  f49b
   1530  f49b			   a5 d6			  LDA	a
   1531  f49d			   c9 ff			  CMP	#255
   1532  f49f			   d0 06			  BNE	.skipL02
   1533  f4a1				   .condpart1
   1534  f4a1			   a5 d8			  LDA	c
   1535  f4a3			   c5 d7			  CMP	b
   1536  f4a5			   90 0c			  bcc	.moosenipple
   1537  f4a7				   .skipL02
On the other hand, the fake for next that you posted is longer than the real for next:

   1515  f48f				   .L01 	;  for x  =  1 to 20
   1516  f48f
   1517  f48f			   a9 01			  LDA	#1
   1518  f491			   85 ed			  STA	x
   1519  f493				   .L01forx
   1520  f493				   .L02 	;  a  =  a  +	1
   1521  f493
   1522  f493			   e6 d6			  INC	a
   1523  f495				   .L03 	;  next
   1524  f495
   1525  f495			   a5 ed			  LDA	x
   1526  f497			   c9 14			  CMP	#20
   1527  f499
   1528  f499			   e6 ed			  INC	x
   1529  f49b			   90 f6			  bcc	.L01forx
   1530  f49d				   .
   1531  f49d						;
   1532  f49d
   1533  f49d				   .L04 	;  temp5  =  0  :  temp6  =  20
   1534  f49d
   1535  f49d			   a9 00			  LDA	#0
   1536  f49f			   85 a0			  STA	temp5
   1537  f4a1			   a9 14			  LDA	#20
   1538  f4a3			   85 a1			  STA	temp6
   1539  f4a5				   .fingernailpudding
   1540  f4a5						; fingernailpudding
   1541  f4a5
   1542  f4a5				   .L05 	;  temp5  =  temp5  +	1
   1543  f4a5
   1544  f4a5			   e6 a0			  INC	temp5
   1545  f4a7				   .L06 	;  a  =  a  +	1
   1546  f4a7
   1547  f4a7			   e6 d6			  INC	a
   1548  f4a9				   .L07 	;  if temp5  <  temp6 then goto fingernailpudding
   1549  f4a9
   1550  f4a9			   a5 a0			  LDA	temp5
   1551  f4ab			   c5 a1			  CMP	temp6
   1552  f4ad			   b0 03			  BCS	.skipL07
   1553  f4af				   .condpart0
   1554  f4af			   4c a5 f4 		  jmp	.fingernailpudding
   1555  f4b2
   1556  f4b2				   .skipL07
But you can do it in the same amount of space and time if you rearrange it, drop temp6, and leave off the goto:

   1515  f48f				   .L01 	;  for x  =  1 to 20
   1516  f48f
   1517  f48f			   a9 01			  LDA	#1
   1518  f491			   85 ed			  STA	x
   1519  f493				   .L01forx
   1520  f493				   .L02 	;  a  =  a  +	1
   1521  f493
   1522  f493			   e6 d6			  INC	a
   1523  f495				   .L03 	;  next
   1524  f495
   1525  f495			   a5 ed			  LDA	x
   1526  f497			   c9 14			  CMP	#20
   1527  f499
   1528  f499			   e6 ed			  INC	x
   1529  f49b			   90 f6			  bcc	.L01forx
   1530  f49d				   .
   1531  f49d						;
   1532  f49d
   1533  f49d				   .L04 	;  temp5  =  1
   1534  f49d
   1535  f49d			   a9 01			  LDA	#1
   1536  f49f			   85 a0			  STA	temp5
   1537  f4a1				   .fingernailpudding
   1538  f4a1						; fingernailpudding
   1539  f4a1
   1540  f4a1				   .L05 	;  a  =  a  +	1
   1541  f4a1
   1542  f4a1			   e6 d6			  INC	a
   1543  f4a3				   .L06 	;  temp5  =  temp5  +	1
   1544  f4a3
   1545  f4a3			   e6 a0			  INC	temp5
   1546  f4a5				   .L07 	;  if temp5  <=  20 then fingernailpudding
   1547  f4a5
   1548  f4a5			   a9 14			  LDA	#20
   1549  f4a7			   c5 a0			  CMP	temp5
   1550  f4a9			   b0 f6			  bcs	.fingernailpudding
Michael

#9 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • 20,911 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Mon Oct 27, 2008 11:45 PM

Thanks. That's some good stuff to know.

#10 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • 20,911 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Mon Oct 27, 2008 11:55 PM

By the way, is there a guideline about how far too far is when not using goto with an if-then?

#11 SeaGtGruff OFFLINE  

SeaGtGruff

    River Patroller

  • 4,543 posts
  • Location:Georgia, USA

Posted Tue Oct 28, 2008 12:28 AM

View PostRandom Terrain, on Tue Oct 28, 2008 1:55 AM, said:

By the way, is there a guideline about how far too far is when not using goto with an if-then?
Yes, it must be within plus or minus 128 bytes of the next statement after the if, since a branch can only go forward 127 bytes or backwards 128 bytes.

Michael

#12 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • 20,911 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Tue Oct 28, 2008 12:30 AM

View PostSeaGtGruff, on Tue Oct 28, 2008 1:28 AM, said:

View PostRandom Terrain, on Tue Oct 28, 2008 1:55 AM, said:

By the way, is there a guideline about how far too far is when not using goto with an if-then?
Yes, it must be within plus or minus 128 bytes of the next statement after the if, since a branch can only go forward 127 bytes or backwards 128 bytes.
Thanks. I need to fit that in somewhere on the bB page.

#13 SeaGtGruff OFFLINE  

SeaGtGruff

    River Patroller

  • 4,543 posts
  • Location:Georgia, USA

Posted Tue Oct 28, 2008 10:03 PM

View PostRandom Terrain, on Tue Oct 28, 2008 1:30 AM, said:

View PostSeaGtGruff, on Tue Oct 28, 2008 1:28 AM, said:

View PostRandom Terrain, on Tue Oct 28, 2008 1:55 AM, said:

By the way, is there a guideline about how far too far is when not using goto with an if-then?
Yes, it must be within plus or minus 128 bytes of the next statement after the if, since a branch can only go forward 127 bytes or backwards 128 bytes.
Thanks. I need to fit that in somewhere on the bB page.
The best place would be in the section about smartbranching, because what the smartbranching option does is let you do something like "if a=1 then reset_the_game" instead of "if a=1 then goto reset_the_game," and the bB compiler will pick the best type of "then"-- "then routine_name" or "then goto routine_name." In other words, smartbranching lets you write your code as if you want to always use the shorter method, but you don't have to worry about whether the place you want to branch to is too far away, because bB will automatically handle it correctly for you.

Michael

#14 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • 20,911 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Wed Oct 29, 2008 2:20 AM

View PostSeaGtGruff, on Wed Oct 29, 2008 12:03 AM, said:

View PostRandom Terrain, on Tue Oct 28, 2008 1:30 AM, said:

View PostSeaGtGruff, on Tue Oct 28, 2008 1:28 AM, said:

View PostRandom Terrain, on Tue Oct 28, 2008 1:55 AM, said:

By the way, is there a guideline about how far too far is when not using goto with an if-then?
Yes, it must be within plus or minus 128 bytes of the next statement after the if, since a branch can only go forward 127 bytes or backwards 128 bytes.
Thanks. I need to fit that in somewhere on the bB page.
The best place would be in the section about smartbranching, because what the smartbranching option does is let you do something like "if a=1 then reset_the_game" instead of "if a=1 then goto reset_the_game," and the bB compiler will pick the best type of "then"-- "then routine_name" or "then goto routine_name." In other words, smartbranching lets you write your code as if you want to always use the shorter method, but you don't have to worry about whether the place you want to branch to is too far away, because bB will automatically handle it correctly for you.
Thanks. Here's a question about smartbranching. The bB info says that smartbranching makes the assembly code harder to read. Is that the only bad thing about it? Does it end up making a program run slower? If not, should we just use it all of the time in our programs so we don't have to waste time figuring out when to use 'then goto' by trial and error?

#15 SeaGtGruff OFFLINE  

SeaGtGruff

    River Patroller

  • 4,543 posts
  • Location:Georgia, USA

Posted Wed Oct 29, 2008 7:26 AM

View PostRandom Terrain, on Wed Oct 29, 2008 4:20 AM, said:

Thanks. Here's a question about smartbranching. The bB info says that smartbranching makes the assembly code harder to read. Is that the only bad thing about it? Does it end up making a program run slower? If not, should we just use it all of the time in our programs so we don't have to waste time figuring out when to use 'then goto' by trial and error?
No, it shouldn't make the program slower. The reason it can make the assembly harder to read is because it will generate some conditional assembly directives that tell the assembler to do it one way or another depending on how far away the target of the branch is. bB's standard kernel already has a bunch of stuff like that in it, so if you ever look at the .lst file of your program, you'll see a bunch of stuff that can look overwhelmingly complicated if you aren't used to it.

Michael

#16 Random Terrain ONLINE  

Random Terrain

    Visual batari Basic User

  • 20,911 posts
  • Controlled Randomness
    Replay Value
    Nonlinear
  • Location:North Carolina (USA)

Posted Wed Oct 29, 2008 7:38 AM

Thanks. Then I'll just have it turned on all of the time and not worry about it. Nice to know it doesn't slow anything down.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users