3 ms·
Even with the 40% hit rate you can reduce it further by asking what could make it perform faster. "That's great for FizzBuzz to 100, but what if we went to a m
by buro9 14y ago
Even with the 40% hit rate you can reduce it further by asking what could make it perform faster.
"That's great for FizzBuzz to 100, but what if we went to a million? What would you do to make it perform in... I don't know, a quarter of the current execution time?"
Even if they wrote a solution that avoided % or / it is always worth a few minute exploration into how one might optimise code that works today but one day is found to be slower than desired.
- buro9 14y agoIn response to an email asking for an example of a fast fizzbuzz, just avoid division. <html> <body> <script type="text/javascript"> function fastfizzbuzz() { var fizz = 1, buzz = 1, fizzbuzz = 1; for (var i = 1; i <= 100000000; i++) { if (fizzbuzz == 15) { //document.writeln('fizzbuzz<br />'); fizz = 0; buzz = 0; fizzbuzz = 0; } else if(fizz == 3) { //document.writeln('fizz<br />'); fizz = 0; } else if(buzz == 5) { //document.writeln('buzz<br />'); buzz = 0; } else { //document.writeln(i + '<br />'); } fizz++; buzz++; fizzbuzz++; } } function slowfizzbuzz() { for (var i = 1; i <= 100000000; i++) { if (i % 15 == 0) { //document.writeln('FizzBuzz<br />'); } else if (i % 3 == 0) { //document.writeln('Fizz<br />'); } else if (i % 5 == 0) { //document.writeln('Buzz<br />'); } else { //document.writeln(i + '<br />'); } } } var startTime=new Date(); fastfizzbuzz(); var endTime=new Date(); document.writeln('Fast: ' + (endTime - startTime) + 'ms<br />'); startTime=new Date(); slowfizzbuzz(); endTime=new Date(); document.writeln('Slow: ' + (endTime - startTime) + 'ms<br />'); </script> </body> </html>