Jump to content
IGNORED

Which is better? a = a * 2 or a = a + a?


Random Terrain

Recommended Posts

From what im reading on Stackoverflow Andrew is correct for languages like C. However they also say the compiler has an optimizer that makes a decision as to what is the fastest possible way no matter how it's coded. The next question i have, does bB have such an optimizer or is that a C only feature?

Link to comment
Share on other sites

1 hour ago, TwentySixHundred said:

From what im reading on Stackoverflow Andrew is correct for languages like C. However they also say the compiler has an optimizer that makes a decision as to what is the fastest possible way no matter how it's coded. The next question i have, does bB have such an optimizer or is that a C only feature?

bB does have an optimizer, but it isn't advanced enough to know that a+a is the same as a*2. Its purpose is to remove redundant code.

 

That said, yes, a=a*2 is faster and uses less space than a=a+a. The reason is because for A*2, you can just issue a single ASL instruction and A+A requires a CLC and an ADC instruction.

  • Like 1
  • Thanks 2
Link to comment
Share on other sites

30 minutes ago, batari said:

bB does have an optimizer, but it isn't advanced enough to know that a+a is the same as a*2. Its purpose is to remove redundant code.

 

That said, yes, a=a*2 is faster and uses less space than a=a+a. The reason is because for A*2, you can just issue a single ASL instruction and A+A requires a CLC and an ADC instruction.

Thanks for clearing that up and it makes sense now how you have explained it. Some useful information about bB's optimizer ?

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