Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Martin Gregorie Newsgroups: comp.lang.java.programmer Subject: Re: So I did "FizzBuzz", in Java! Date: Mon, 18 Jul 2016 21:59:25 -0000 (UTC) Organization: A noiseless patient Spider Lines: 71 Message-ID: References: <1933e39b-6363-4174-8392-602415a61799@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Mon, 18 Jul 2016 21:59:25 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="e5160e2be48f1ea550cf61512446f525"; logging-data="18358"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+gAw/63H6QfU1OzY+uhmMueD8VGfbK/c8=" User-Agent: Pan/0.140 (Chocolate Salty Balls; GIT 3663720 git://git.gnome.org/pan2) Cancel-Lock: sha1:2Fa6p12KEpiN2XwSZHMGQpSg43Y= Xref: csiph.com comp.lang.java.programmer:34961 On Mon, 18 Jul 2016 14:00:26 -0700, Eric Douglas wrote: > On Monday, July 18, 2016 at 2:10:07 PM UTC-4, Eric Sosman wrote: >> 3) This is not the first "efficiency" question you've asked, which >> leads me to fear that you may be well on the way to becoming an addict. >> Stop! Stop now, while you still can! "Efficiency" is a drug as habit- >> forming and as destructive as anything you can find in a darkened back >> alley. If you get hooked, you'll never get clean. Trust me, I know: >> The monkey has ridden my back for half a century despite my strenuous >> efforts to dislodge it. Don't follow my pathway to ruin! > > It depends how big the efficiency problem is and how many places it's > used. If you're saving a few seconds it may not be an issue, unless > that code is called many times. > I worked for one of the largest retailers in the world and we had some > major performance issues. One program we'd get down to under an hour, > the initial contractors wrote to run over 8 hours.. on my laptop, which runs Linux (Fedora 23) on a 1.6GHz Core Duo, Daniel's code took about twice the elapsed time and used twice the mill time (both user and system mill) as a FizzBuzz version I made which uses a counted for loop to drive it. I was careful to use exactly the same logic as Daniel used inside the loop and the same lifetime for each StringBuilder instance. I've now added a command line parameters so I can run much longer series The looped version remains fastest at up to 100,000 tries, but not by so much: 100 numbers Original looped real time .399s .205s user cpu .480s .188s system cpu .055s .027s 100,000 numbers Original looped real time .901s .687s user cpu 1.382s .934s system cpu .143s .125s I'm really intrigued, though by the way that the cpu usage increases more for the looped code than for Daniel's original. Here's my code: public class FizzBuzz2 { public static void main(String[] args) { int n = 100; if (args.length > 0) n = Integer.parseInt(args[0]); System.out.println("Run from 1 to " + n); for (int i = 1; i <= n; i++) { 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); System.out.println(result.toString()); } } } -- martin@ | Martin Gregorie gregorie. | Essex, UK org |