2

I had a challenge to print out multiples of 7 (non-negative) to the 50th multiple in the simplest way humanly possible using for loops.

I came up with this (Ignoring the data types)

for(int i = 0; i <= 350; i += 7)
         {System.out.println(i);}

The other guy came up with this

for(int i=0;i <=50; i++)
{
System.out.println(7*i);
}

However, I feel the two code snippets could be further optimized. If it actually can please tell. And what are the advantages/disadvantages of one over the other?

4
  • 2
    You want to optimize this? The output part will take roughly 99.99% of the time, with the actual math taking way too little time to even spend a single thought on whether to optimize it, much less how. By the time you arrive at enlightenment and realize all that and even more reasons you shouldn't worry,, you've already spent countless times the time it takes to make these calculations thousands and thousands of times. Way to waste time ;) Commented Jul 24, 2011 at 22:02
  • 3
    there's for(int i=0; i<1; i++) System.out.println("0,7,...."); // :-) Commented Jul 24, 2011 at 22:11
  • The fastest execution would be to unroll the loops and just print each number: System.out.println("0"); System.out.println("7"); System.out.println("14"); ... . But you ruled that out in the premise to the challenge. :-P However, "the simplest way humanly possible" can mean many different things; I would it to mean "easiest to maintain" and/or "easiest to understand what's going on by looking at the code," not "fastest." Commented Jul 24, 2011 at 22:12
  • @delnan: I with I could find the comment where I first heard the expression "getting a haircut to lose weight". Commented Jul 25, 2011 at 0:18

4 Answers 4

9

If you really want to optimize it, do this:

System.out.print("0\n7\n14\n21\n28\n35\n42\n49\n56\n63\n70\n77\n84\n91\n98\n105\n112\n119\n126\n133\n140\n147\n154\n161\n168\n175\n182\n189\n196\n203\n210\n217\n224\n231\n238\n245\n252\n259\n266\n273\n280\n287\n294\n301\n308\n315\n322\n329\n336\n343\n350");

and it's O(1) :)

Sign up to request clarification or add additional context in comments.

1 Comment

++ Even faster would be to have the results in a file, and just cat it.
3

The first one technically performs less operations (no multiplication).

The second one is slightly more readable (50 multiples of 7 vs. multiples of 7 up to 350).

Probably can't be optimized any further.

Unless you're willing to optimize away multiple println calls by doing:

StringBuilder s = new StringBuilder();

for(int i = 0; i <= 350; i += 7) s.append(i).append(", ");

System.out.println(s.toString());

(IIRC printlns are relatively expensive.)

This is getting to the point where you gain a tiny bit of optimization at the expense of simplicity.

16 Comments

But i++ is faster than i+7.
@fastcodejava: Says who? And even if, could you measure a difference even for insanely many iterations?
@fastcodejava - How do you know that i++ is always faster than i+7?
But CPU's multiplication is basically addition??
@fastcodejava: doing i++ and then i*7 is almost certainly more expensive than just doing i+=7.
|
1

In theory, your code is faster since it does not need one less multiplication instruction per loop.

However, the multiple calls to System.out.println (and the integer-to-string conversion) will dwarf the runtime the multiplication takes. To optimize, aggregate the Strings with a StringBuilder and output the whole result (or output the result when memory becomes a problem).

However, in real-world code, this is extremely unlikely to be the bottleneck. Profile, then optimize.

Comments

0

The second function is the best you would get: O(n)

4 Comments

@Both functions are O(n), and the constant of the asymptotic complexities is actually meaningful here.
First function is not O(n)?
I know they are both O(n) the second one makes fewer iterations thus more optimized. And when scaling even greater optimized
Both make exactly the same number of iterations, only with different step sizes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.