+Random Terrain #1 Posted January 4, 2020 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)? Quote Share this post Link to post Share on other sites
+Random Terrain #2 Posted January 5, 2020 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? Quote Share this post Link to post Share on other sites
TwentySixHundred #3 Posted January 5, 2020 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 1 Quote Share this post Link to post Share on other sites
Andrew Davie #4 Posted January 5, 2020 Shifting is faster for powers of two. I don't know BB syntax but something like a<<=1, or a = a<<1 2 1 Quote Share this post Link to post Share on other sites
TwentySixHundred #5 Posted January 5, 2020 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? Quote Share this post Link to post Share on other sites
+batari #6 Posted January 5, 2020 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. 1 2 Quote Share this post Link to post Share on other sites
TwentySixHundred #7 Posted January 5, 2020 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 👍 Quote Share this post Link to post Share on other sites