1

I'd like for efficiency sake to ask the following question. In, for example C you would declare a "global" variable if you wanted to re-use the same variable in functions over and over again without the added cost of converting it to a local variable (as that would require to re-initialize that variable when we call that function again).

In Java I am not so sure what would be best, have a local variable and re-initialize it over and over (maybe the optimizer is smart enough to remember it?) or declare it as a private variable separately inside the class? My gut tells me the latter should be better (and that is what I am currently doing) but I am not entirely certain that is the case.

Of course please exclude the multi-threading scenario where atomicity would be a thing.

jtimz
  • 324
  • 3
  • 14
  • 1
    Idk if I understand your question well. But use it as a class variable if it adds some character to class and all objects would want to have it. use it as a local variable if it is being used temporarily by the function for some calculations etc. – Mukul Goel Mar 19 '15 at 11:54
  • Java and C have very similar syntax but very different concept and structure. In C you can have global variables to be defined anywhere in one of your 300 source code file, not necessarily in the one with the main(). They can be used in any of these 300. In Java, your main() is just a method in one of your classes that JVM handles. Your class can have static variables that survive just within the class. But still, to the JVM, they are not "global variables" in C's sense. They are just "local" variable "local" to the class. – senderj Mar 20 '15 at 07:52
  • C was born when hardware were expensive. That's why they have pointer, address, pass by address. And C is good at performance. Java was born at very different age when hardware is relatively cheap. Observing performance is a good practice but not as essential as for C. I am not very experience at both, so I would appreciate any correction on my comment so that I can learn. – senderj Mar 20 '15 at 07:56

4 Answers4

3

I think that answer might help you. He wrote a micro-benchmark for determine the access speed of local and instance variables.

The result showed that local variable accesses are about 1% faster than instance variable accesses (even if both point to the same object).

Community
  • 1
  • 1
angeldev
  • 1,983
  • 3
  • 18
  • 29
  • I have to note (and it is noted in the answer you linked) that those differences are probably not representative and very small. Do not base code design decisions upon benchmarks like this. – LionC Mar 19 '15 at 12:48
1

Do not try to tune performance on such a low level. The performance of the resulting code depends heavily on the compiler anyways, but such a small "improvement" will not have any significant increase at all, as you are only talking about assigning a simple value to a variable, which does not take significant time to do (if we are talking about complex objects being created every call, that is obviously different, but your question was about initializing variables, so I answer that).

However, having a variable that is only relevant for a single function as a field of a class really hurts the readability and self-expression of your code, as the field is not relevant for an instance of that class at all.

Conclusion: If a variable is a local function variable, put it into the local function scope. If it is a field, put it as a field. Do not try to tune performance that way.

LionC
  • 3,106
  • 1
  • 22
  • 31
  • That variable might have a significant memory footprint though, I didn't specify the type of it and "local" copies might have a significant impact on the GC intervals. – jtimz Mar 19 '15 at 12:07
  • @jtimz as I said, I was talking about variables(which are just an adress or a value and have no footprint at all). If you are talking about creating the exact same (maybe large) Object every call, that is a different story (and in most cases means that it should maybe be a field anyways). But assigning a variable (aka assigning a reference to an already existing object or assigning a primitive value) is not relevant performance-wise. If you clear that up for me, the answer might be different (as would the question) – LionC Mar 19 '15 at 12:13
-1

You cannot directly convert all you local method variables to instance variables. You ll break your code if you do so.

Instance variable are for specific purpose - which defines the state of an object. What you are asking can be solve by using static variables(probably final also - depending on the situation). If you use static variables - it ll be available for all the objects you create. But it will compromise with the thread safety as any object can modify it.

shikjohari
  • 2,278
  • 11
  • 23
  • As I said I don't talk about the multi-threading scenario, but I think there are some performance benefits; I also mentioned only instance variables as I'd like each class to have it's own local copy. – jtimz Mar 19 '15 at 12:06
-2

Private variable are accessible within the class while local function variable are accessible only within the function.

Ashwani Verma
  • 39
  • 1
  • 11
  • 1
    The question was not about the accesibility of fields vs locals, it was about what is better ("for the sake of efficiency"). Please take your time to read the question before answering. – LionC Mar 19 '15 at 12:26