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?

  • 3
    Possible duplicate of Is micro-optimisation important when coding? – gnat Oct 6 '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 6 '17 at 13:55
  • Sorry for confusion! But thankfully, I've got my answer! – Anubhav Das Oct 6 '17 at 14:01
  • What do they teach people in school these days? – user251748 Oct 6 '17 at 16:46
  • Well not everyone takes a lesson at school... – Anubhav Das Oct 6 '17 at 18:05

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.

| improve this answer | |
  • Thanks! I got the idea! And it was wrong to include the C tag, sorry! – Anubhav Das Oct 6 '17 at 12:58

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.

| improve this answer | |
  • Right! Using local variables makes the code easier to read, as compared to using multiple method calls. – Anubhav Das Oct 6 '17 at 14:05

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.

| improve this answer | |
  • I prefer the same approach too, it makes the code easier to read and debug. – Anubhav Das Oct 7 '17 at 3:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.