Update
This commit is contained in:
parent
f354398fd2
commit
53768ca2ea
20 changed files with 51 additions and 29 deletions
|
@ -15,7 +15,7 @@ TODO: some history etc.
|
|||
|
||||
## Implementations
|
||||
|
||||
Let's see how we can implement, improve and [optimize](optimization.md) FizzBuzz in [C](c.md). Keep in mind the question of scalability, i.e. try to imagine how the changes we make to the algorithm would manifest if the problem grew, i.e. if for example we wanted to check divisibility by many more numbers than just 1 and 5 etc. We will only focus on optimizing the core of the algorithm, i.e. the divisibility checking, without caring about other things like optimizing printing the commas between numbers and whatnot. Also we'll be supposing all compiler optimization are turned off so that you the excuse "compiler will optimize this" can't be used :)
|
||||
Let's see how we can implement, improve and [optimize](optimization.md) FizzBuzz in [C](c.md). Keep in mind the question of scalability, i.e. try to imagine how the changes we make to the algorithm would manifest if the problem grew, i.e. if for example we wanted to check divisibility by many more numbers than just 1 and 5 etc. We will only focus on optimizing the core of the algorithm, i.e. the divisibility checking, without caring about other things like optimizing printing the commas between numbers and whatnot. Also we'll be supposing all compiler optimization are turned off so that the excuse "compiler will optimize this" can't be used :)
|
||||
|
||||
For starters let us write a kind of vanilla, [naive](naive.md) solution that everyone will likely come up with as his first attempt. A complete noob will fail to produce even this basic version, a slightly advanced programmer (we might say a "[coder](coding.md)") may submit this as the [final solution](final_solution.md).
|
||||
|
||||
|
@ -44,7 +44,7 @@ int main(void)
|
|||
}
|
||||
```
|
||||
|
||||
It [works](just_werks.md), however with a number of issues. Firstly we see that for every number we check we potentially test the divisibility by 3 and 5 twice, which is not good, considering division (and modulo) are one of the slowest instructions. We also reuse the magic constants 3 and 5 in different places, which would start to create a huge mess if we were dealing with many more divisors. There is also a lot of branching, in the main divisibility check we may jump up to three times for the checked number -- jump instruction are slow and we'd like to avoid them (again, consider we were checking e.g. divisibility by 1000 different numbers).
|
||||
It [works](just_werks.md), however with a number of issues. Firstly we see that for every number we check we potentially test the divisibility by 3 and 5 twice, which is not [good](good.md), considering division (and modulo) are one of the slowest instructions. We also reuse the magic constants 3 and 5 in different places, which would start to create a huge mess if we were dealing with many more divisors. There is also a lot of branching, in the main divisibility check we may jump up to three times for the checked number -- jump instruction are slow and we'd like to avoid them (again, consider we were checking e.g. divisibility by 1000 different numbers).
|
||||
|
||||
When asked to optimize the algorithm a bit one might come up with something like this:
|
||||
|
||||
|
@ -126,6 +126,7 @@ int main(void)
|
|||
if (i != 1)
|
||||
printf(", ");
|
||||
|
||||
// look mom, no branches!
|
||||
char *s = str;
|
||||
|
||||
*s = '1'; // convert number to string
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue