2

I do not have a teacher who I can ask questions about efficiency, so I will ask it here.

If I am only looking to have fast working code, not paying attention to ram use, only cpu:

I assume that checking 'if' once is faster than writing a variable once. But what is the ratio? When is it worth always checking if the variable is not already at the value that I am going to set it to?

For example:

//ex. 1
int a = 5;
while (true) {
    a = 5;
}

//ex. 2
int a = 5;
while (true) {
    if (a != 5) a = 5;
}

//ex. 3
int a = 6;
while (true) {
    if (a != 5) a = 5;
    a = 6;
}

I guess ex. 2 will work faster than ex. 1 because 'a' always stays at '5'. In this case 'if' speeds up the process by not writing a new value to 'a' everytime. But if 'a' often changes, like in ex. 3, then checking if (a != 5) is not necessary and slows down the process. So this checking is worth it if the variable stays the same most of the time; and not worth it if the variable changes most of the time. But where is the ratio? Maybe writing a variable takes 1000 times more time than just checking it? Or maybe writing almost takes the same time as checking it? Im not asking for an exact answer, I just always wonder what is best for my code.

James Allison
  • 135
  • 10
  • 3
    Do you know JVM will change your code and it won't look the same? – Sajal Dutta Apr 04 '14 at 13:13
  • You might want to switch to assembler and read the two links suggested here: http://stackoverflow.com/questions/692718/how-to-find-cpu-cycle-for-an-assembly-instruction – Augusto Apr 04 '14 at 13:15
  • 1
    Write code that is readable instead of trying to optimize that kind of thing. Sidenote: `if` can be rather expensive since it can mess up [branch prediction](http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array). Execution of 1 `if` can cost you several instructions (Depending on pipeline length of your processor). – zapl Apr 04 '14 at 13:21

1 Answers1

8

Short answer: it doesn't matter.

Long answer: It really doesn't matter at that low level. Even if you were to actually compare the executed machine code, there are so many things in between (the JIT compiler for one, all sorts of CPU caches for other).

Gone are the times when you needed to micro-optimize things like this. What you need to make sure is that you're using effective algorithms. And as always, premature optimization is the root of all evil.

I noted that you wrote "I just always wonder what is the best way for my code". The best way is to write clear code, so that other people can understand what you're doing (if they saw code like in your examples, they would think you're insane). Another old adage was that in order for the JVM to optimize your code in the best way, you should write "dumb code". The JIT optimizer can then understand the code better and convert it to a more efficient form.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • 2 options: "check 1,000,000 times and write 1,000,000 times." or "just write 1,000,000 times." obviously the second one should work faster right? – user2757036 Apr 04 '14 at 13:19
  • @user2757036 It likely won't make a difference. That's the whole point of this answer. – arshajii Apr 04 '14 at 13:22
  • 2
    There's nothing obvious about anything here. If you don't know how compiler optimizations and runtime optimizations work (let's ignore the CPU caches for now), I wouldn't recommend that you think anything is "obvious". – Kayaman Apr 04 '14 at 13:23
  • Simple code, without any "clever tricks". If I tell you to optimize this: "1+2+3", you can easily see that it can be written as "3+3". If I tell you to optimize this: "1+1+1+1+1+1", you'll have a harder time. My justification for using "1+1+1+.." could be that "1+1" is a faster operation than "1+anything else", so I would try to use this sort of "clever trick". – Kayaman Apr 04 '14 at 13:32
  • 1
    +1 @user2757036: Just to add two cents, I guess it takes experience to learn what you should worry about. Some people here think to lose weight requires getting a haircut. This is like wondering if you should trim the top or side hairs. To maximize performance, don't look for generalities. You have to start with a specific program that runs, then find out what it is doing that could be done without. – Mike Dunlavey Apr 04 '14 at 13:39