2

There are situations where we can store values in local variables and work with them. For example:

String data;
int a, b;

data = Integer.toHexString(memory.read(PC));
a = ("00" + data).substring(data.length());

data = Integer.toHexString(memory.read(PC + 1));
b = ("00" + data).substring(data.length());

Or we can just make multiple function calls to achieve the same thing:

a = ("00" + Integer.toHexString(memory.read(PC))).substring(Integer.toHexString(memory.read(PC)).length());
b = ("00" + Integer.toHexString(memory.read(PC + 1))).substring(Integer.toHexString(memory.read(PC + 1)).length());

Which one is better from the performance point of view?

Deduplicator
  • 8,591
  • 5
  • 31
  • 50
Anubhav Das
  • 133
  • 4
  • 3
    Possible duplicate of [Is micro-optimisation important when coding?](https://softwareengineering.stackexchange.com/questions/99445/is-micro-optimisation-important-when-coding) – gnat Oct 06 '17 at 12:12
  • 1
    Best of all would be to give those local variables meaningful names. Your code example seems to be parsing some kind of information obtained by calling `memory.read(...)`. Give the variables names that explain the nature or the purpose of that information. – Solomon Slow Oct 06 '17 at 13:55
  • Sorry for confusion! But thankfully, I've got my answer! – Anubhav Das Oct 06 '17 at 14:01
  • What do they teach people in school these days? –  Oct 06 '17 at 16:46
  • Well not everyone takes a lesson at school... – Anubhav Das Oct 06 '17 at 18:05
  • An important detail to remember is that caching the values in a local variable would ignore any underlying changes in the data; so if `memory.read` could return different values for the same inputs over time, these would not be equivalent implementations. – Joe Oct 11 '17 at 00:50
  • You highlighted an important point! But luckily in this case, they aren't cached! – Anubhav Das Oct 11 '17 at 02:45

3 Answers3

3

Go with the version that's easiest to read and understand.

I agree with Alex's answer - it's very unlikely that there will be a measurable difference in performance between the two versions.

However, even if there was a difference you can easily lose all of those gains while the app is down and you're trying to debug it.

Dan Pichelman
  • 13,773
  • 8
  • 42
  • 73
2

There is a performance difference. Integer.toHexString(memory.read(_)) is called 2 times in the first example, and 4 times in the second example. Since your question was more about the general case, if using the variables causes half the calls to occur, and the calls are slow (not in this specific case), it can be twice as slow to use the first method.

It is personal taste, but I prefer the common sub-expressions to be factored out using local variables, both to ensure they have the same value in all places referenced, and to make the common nature of the sub-expression more clear.

Frank Hileman
  • 3,922
  • 16
  • 18
1

The best way to answer the question is to put both fragments into large-ish loops and time them a few times.

The unscientific answer is that it's fairly unlikely (in Java) that they'll differ in performance all other things being equal. During the compilation process, the compiler will likely convert the unassigned method calls to assignments to temporaries anyway. As such, the two will end up being identical (plus or minus some statement permutations) and end up as equivalent bytecode.

You also have C as a tag. As this is definitely not C code, I can't really comment on what would happen in C. You could conceivably write this in C++ but then the answer would depend a lot on the definition of the String class, amongst others, so this can't be answered in general.

Alex
  • 3,882
  • 1
  • 15
  • 16