Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #34972
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2016-07-19 05:06 -0700 |
| References | (1 earlier) <nmj00i$jae$1@dont-email.me> <1933e39b-6363-4174-8392-602415a61799@googlegroups.com> <nmj61j$95o$1@dont-email.me> <cc1c596f-a043-4ba7-9e55-b98fc12cc89c@googlegroups.com> <nmjhqe$jj6$1@dont-email.me> |
| Message-ID | <d30c2b2f-0471-4231-9867-19126e5edc13@googlegroups.com> (permalink) |
| Subject | Re: So I did "FizzBuzz", in Java! |
| From | Eric Douglas <e.d.programmer@gmail.com> |
On Monday, July 18, 2016 at 5:31:03 PM UTC-4, Eric Sosman wrote:
> - Finally, observe that by transforming a correct program from
> IntStream to `for' loop you produced an *incorrect* program!
> Faster, yes (even without measurement I'm confident of that),
> but incorrect nonetheless. What price "efficiency," hmm?
Who me bad code? I just tested the code I modified and it runs fine. It's just missing the print requirement.
public final class FizzBuzz {
public static void main(final java.lang.String[] args) {
for (int i = 1; i <= 100; i++) {
final StringBuilder result = new StringBuilder();
if (0 == i % 3) {
result.append("Fizz");
}
if (0 == i % 5) {
result.append("Buzz");
}
if (result.length() == 0) {
result.append(i);
}
java.lang.System.out.println( result );
}
}
}
I ran the IntStream version 3 times with a timer. It took 5762, 5817, 5826 milliseconds to go through 1 to 1,000,000.
I ran this for loop 1 to 1,000,000 with the same timer 3 times. It took 5715, 5803, 5685 milliseconds.
So yes it is faster without the fancy IntStream. Is it worth changing production code? It would have to be really heavily used to get noticeable savings on the performance. Is it more readable/maintainable? I think that is a much more important question when performance is very similar and functionality remains the same.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: So I did "FizzBuzz", in Java! Daniel Pitts <usenet@coloraura.com> - 2016-07-18 09:26 -0700
Re: So I did "FizzBuzz", in Java! Eric Douglas <e.d.programmer@gmail.com> - 2016-07-18 10:10 -0700
Re: So I did "FizzBuzz", in Java! Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-07-18 14:09 -0400
Re: So I did "FizzBuzz", in Java! Eric Douglas <e.d.programmer@gmail.com> - 2016-07-18 14:00 -0700
Re: So I did "FizzBuzz", in Java! Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-07-18 17:30 -0400
Re: So I did "FizzBuzz", in Java! Eric Douglas <e.d.programmer@gmail.com> - 2016-07-19 05:06 -0700
Re: So I did "FizzBuzz", in Java! Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-07-19 08:13 -0400
Re: So I did "FizzBuzz", in Java! Eric Douglas <e.d.programmer@gmail.com> - 2016-07-19 05:18 -0700
Re: So I did "FizzBuzz", in Java! Martin Gregorie <martin@address-in-sig.invalid> - 2016-07-18 21:59 +0000
Re: So I did "FizzBuzz", in Java! Arne Vajhøj <arne@vajhoej.dk> - 2016-07-18 19:51 -0400
Re: So I did "FizzBuzz", in Java! Arne Vajhøj <arne@vajhoej.dk> - 2016-07-18 19:46 -0400
csiph-web