aspose file tools*
The moose likes Performance and the fly likes Probably it may be really silly question for others but not for me. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Probably it may be really silly question for others but not for me." Watch "Probably it may be really silly question for others but not for me." New topic
Author

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

Hardik Trivedi
Ranch Hand

Joined: Jan 30, 2010
Posts: 252
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.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

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

Joined: Jan 30, 2010
Posts: 252
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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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 Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

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

Joined: Jan 30, 2010
Posts: 252
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 Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

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

Joined: Jan 30, 2010
Posts: 252
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.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11475
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18885
    
    8

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

Joined: Jan 30, 2010
Posts: 252
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

Joined: Jan 30, 2010
Posts: 252
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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8196
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Depending on your system, nanoTime() may not be noticeably more accurate than currentTimeMillis() though.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8196
    
  23

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
Rojan punn
Greenhorn

Joined: Nov 29, 2011
Posts: 17


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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Probably it may be really silly question for others but not for me.