Jump to content
Random Terrain

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

Recommended Posts

If 'a' starts out with a value of one, a = a * 2 would give you the same thing as a = a + a, right? So which one is faster or uses less code (or both)?

Share this post


Link to post
Share on other sites

When I run both through the Routine Size Comparison Template, the a = a + a section gives a 7, and the a = a * 2 section gives a 5. The multiplication version uses less space. How do we figure out which one is faster?

Share this post


Link to post
Share on other sites

What was the value of 'a' and why the two different results? I would assume a = a + a is faster as it's addition however i could be wrong. Interested to see the results

  • Like 1

Share this post


Link to post
Share on other sites

Shifting is faster for powers of two. I don't know BB syntax but something like a<<=1, or a = a<<1

  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
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

Share this post


Link to post
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 👍

Share this post


Link to post
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.

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