Jump to content
IGNORED

Which if-then is faster?


Random Terrain

Recommended Posts

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.

Link to comment
Share on other sites

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

 

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.randomterrain.com/personal-movies-tv-f-m.html

 

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

Link to comment
Share on other sites

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

  • Like 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

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