• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Probably it may be really silly question for others but not for me.

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

As subject lines says probably it may be really silly question for others but not for me.
The question is does length of name of variable affects performance or size of program ?

Now I know bit of compiler and code builder.They have mapping of variable name and data types.They called them symbol table.
Its has very high level of computing and implementation of complex data structures.

Now lets assume I declare a variable named 'a' and 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'. So it must be storing these names and they require memory.

So I think long variable names require more memory to store temporary or permanent.

Now you can start bombarding your view points.
You guys can correct me if I am wrong.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, the subject line you've chosen is not very descriptive. See UseAMeaningfulSubjectLine.

Secondly, the variable names are really used only by the compiler and by the debugger. They are stored separately from the actual code and during normal execution they do not affect the execution whatsoever. They must be stored somewhere in the class file to support reflection. Depending on how JVM manages this information from class files, shorter variable names might speed up access via reflection a little bit; however if you've performance problems caused by reflection, the problem does not reside in the length of variable names.

The compiler probably puts identifiers it encounters into some sort of a structure like HashMap, so I'd be surprised if shortening the variable (method, class etc.) names to single letters would affect the compilation time noticeably. On the other hand, it would certainly negatively affect the development time, as programs with unintelligible identifiers is a catastrophe to maintain.

This leads to my third point - even if using short variable names sped up some part of what the computer does (which I doubt), it is quite clearly not a good thing to do. It is probably the most egregious example of misplaced "optimization" anyone might do. You should always choose names that are as descriptive as possible. This is usually covered by programming conventions and would probably be a topic of another discussion.
 
Hardik Trivedi
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Martin,

I am not opposing the things that we should not name variable properly. I also named variable properly in my applications. The thing was just about the knowledge and just to share with others.
Second the time taken by compiler to identify variable certainly will not affect the overall time much.But the thing is it will affect for sure.

I am again saying that I just put a strange question in from of people.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hardik Trivedi wrote:Hi All,

As subject lines says probably it may be really silly question for others but not for me.
The question is does length of name of variable affects performance or size of program ?



No.

If you spend 2 more seconds thinking about this, you are wasting your time.

Seriously.

No, I mean it. For real. It took you 1,000 time longer to read this than the absolute most possible performance gain you could achieve in an ideal world by shortening your variable names from 100 characters to 1.

Also, don't forget that programmers are magnitudes of order more expensive than hardware, so if it takes your programmer 5 seconds to figure out what variable "x" is, vs. 5 nanoseconds difference between the JVM figuring out variable "x plus 100 characters" vs. variable "x", you lose.

Make your code easy to write, read, understand test, debug, maintain, and enhance.
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion, this topic (with the emphasis on the speed) is a perfect example of "premature optimization". That has been discussed many times on this very forum. (I've my own invaluable, entertaining, personal experience with premature optimization: many years I was - just for fun - coding GIF reader in assembler, and designed some clever optimizations right from the start. The result was somewhat feeble. After replacing the "clever" code with a straight one, the routine got three times shorter and two times faster.)

If you are interested in the inner workings of JVM and the compiler, you might - for example - read about the class file structure. A cursory search brought up some pages, but I'm not sure they were the latest version, so I'm not going to put the link here.

Anyway, I'm pretty sure that your original question is not that interesting. I've medium-sized project, and the compiling bottleneck is HDD, not CPU. Shaving off a few milliseconds of CPU time (which is in itself improbable) would not gain any speedup at all for me. The memory is also not a concern - all sources in my project have 13 MB in total. So even by reducing all names to single letters I couldn't save more than some 13 MB of memory, in reality much much less (ya know, I've also a few comments - totally skipped by the compiler - here and there ). 13 MB is not worth a single word given today's memory prices and typical computer configurations.
 
Hardik Trivedi
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well well well , Jeff and Martin.


I never said that I want to reduce size of my code.
And I know very well that its a silly question and that what I mention in the subject.
I also said that I also use proper variable name.
I am not telling that we should use one character variable name.

Why you guys are calculating so much.


What I want to know is whether it affects the time or not ?

And answer to this can be yes or no only....

I got my answer by the way
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hardik Trivedi wrote:What I want to know is whether it affects the time or not ?

And answer to this can be yes or no only....


Is it really that simple? Lets assume (just assume) that the difference you get this way is 0.001%. Does it affect the time? Physically speaking, (measured with atomic clock ), yes. In all practical aspects, however, the answer is no - the difference in memory and time is so negligible you'll never be able to find out. So the answer actually depends on why you ask. And given you asked in Performance forum, the (maybe somewhat paranoid) concern of actually acting upon the "yes" answer prompted me into qualifying the answer the way I did. After all, you wanted to be "bombarded" with viewpoints. Here you are, then....
 
Hardik Trivedi
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Martin Vajsar wrote:

Hardik Trivedi wrote:What I want to know is whether it affects the time or not ?

And answer to this can be yes or no only....


Physically speaking, (measured with atomic clock ), yes.




I again got my answer.And so as you to.
As I told I do not care about %.
I just want to know yes or no.

Bye. Have a great day.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think the answer is a clear cut 'yes' or 'no'. I have no idea how a hash might work, but it is CONCEIVABLY possible that all variable names need to be normalized to the same length to do the calculation. Perhaps the default is 20 bytes.

So, if you name a variable with only one character, the JVM has to allocate 20 bytes, copy the data over, pad the missing bytes, calculate the hash, then free the un-needed space.

Whereas if you named all your variables exactly 20 characters, all you have to do is calculate the hash.

So in this case, shorter variable names might SLOW DOWN things.

Yes, this is a silly example that is very contrived, but it is possible.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hardik Trivedi wrote:
What I want to know is whether it affects the time or not ?

And answer to this can be yes or no only....



Not true. There could be other answers, such as "it depends." In this particular case, the answer is no, but the one asking the questions doesn't generally get to dictate what the possible answers can be
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Hardik Trivedi wrote:
What I want to know is whether it affects the time or not ?

And answer to this can be yes or no only....



Not true. There could be other answers, such as "it depends."



Or "who cares?", which is the answer already posted by several people in this thread. Here's another silly question: if I leave my front door open when I leave the house on my way to work, will I get to work faster? Yes or no?
 
Hardik Trivedi
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Jeff Verdegan wrote:

Hardik Trivedi wrote:
What I want to know is whether it affects the time or not ?

And answer to this can be yes or no only....



Not true. There could be other answers, such as "it depends."



Or "who cares?", which is the answer already posted by several people in this thread. Here's another silly question: if I leave my front door open when I leave the house on my way to work, will I get to work faster? Yes or no?




Well if you really think logically If you do not lock the door you will save few seconds thats is for sure.
 
Hardik Trivedi
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:I don't think the answer is a clear cut 'yes' or 'no'. I have no idea how a hash might work, but it is CONCEIVABLY possible that all variable names need to be normalized to the same length to do the calculation. Perhaps the default is 20 bytes.

So, if you name a variable with only one character, the JVM has to allocate 20 bytes, copy the data over, pad the missing bytes, calculate the hash, then free the un-needed space.

Whereas if you named all your variables exactly 20 characters, all you have to do is calculate the hash.

So in this case, shorter variable names might SLOW DOWN things.

Yes, this is a silly example that is very contrived, but it is possible.




Our friend Fred is some good point... We do have limit for the length of variable name.

I typed really a long variable name and when I tried to build the program compiler says "The type generates a string that requires more than 65535 bytes to encode in Utf8 format in the constant pool"

So I will go with the Fred's answer that JVM keeps some predefined memory for all variable declaration.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hardik Trivedi wrote:So I will go with the Fred's answer that JVM keeps some predefined memory for all variable declaration.


No don't. Don't go with Fred's or Jeff's or Paul's or Martin's...or mine, for that matter (which would be the same as theirs).

TEST IT.

Write a program with some long loops (a million iterations might be a good place to start) that uses enormous names, and another that runs the same code with short names and see for yourself. I also suggest you use System.nanoTime(), since I doubt whether the difference will be noticeable otherwise; and run the test several times. In fact, if you do see any significant difference I, for one, would be interested in seeing your results.

You might also want to run a profiler to see if there is any significant difference in memory usage. Again, I doubt it, but I've never actually tried it for myself.

Winston

 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depending on your system, nanoTime() may not be noticeably more accurate than currentTimeMillis() though.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Depending on your system, nanoTime() may not be noticeably more accurate than currentTimeMillis() though.


Very true, but I believe that there is a guarantee that it will use the most accurate timer available; and certainly on my 5 year old workhorse Dell, the difference is significant.

Winston
 
Greenhorn
Posts: 17
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I thought any variables created in java has an address in memory and JVM knows how to get the value and set the value for variable using getField and setfield method using memory address as reference point.
But byte code just document the name of the varibale you create.
Is this wrong?
 
reply
    Bookmark Topic Watch Topic
  • New Topic