Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jump Tables / Binary Search #177

Open
hediet opened this issue Dec 16, 2018 · 0 comments
Open

Jump Tables / Binary Search #177

hediet opened this issue Dec 16, 2018 · 0 comments
Labels
optimization 💪 New optimization feature
Projects
Milestone

Comments

@hediet
Copy link
Member

hediet commented Dec 16, 2018

If we have code like that:

if (x == 10) { ... } // 1 test needed
else if (x == 11) { ... } // 2 tests needed 
else if (x == 50) { ... } // 3 tests needed 
else if (x == 100) { ... } // 4 tests needed
else if (x == 111) { ... } // 5 tests needed
else if (x == 200) { ... } // 6 tests needed
// avg: 3.5 tests needed on average, 6 on worst

We could optimize it to:

if (x < 50) {
   if (x == 10) { ... } // 2 tests needed
   else if (x == 11) { ... } // 3 tests needed
} else {
  if (x < 111) {
   if (x == 50) { ... } // 3 tests needed
   else if (x == 100) { ... } // 4 tests needed
  }
  else {
   if (x == 1111) { ... } // 3 tests needed
   else if (x == 200) { ... } // 4 tests needed
  }
}
// avg: 3.17 tests needed on average, 4 on worst

We could even use jump tables if the values are close enough together.

@hediet hediet added the optimization 💪 New optimization feature label Dec 16, 2018
@joshuabach joshuabach added this to the Optimizations milestone Jan 4, 2019
@problame problame added this to Todo Optimization in Endspurt Feb 4, 2019
@hediet hediet moved this from Todo Optimization to Stretch Goal in Endspurt Feb 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
optimization 💪 New optimization feature
Projects
Endspurt
  
Stretch Goal
Development

No branches or pull requests

2 participants