wood burning stoves 2.0*
The moose likes Performance and the fly likes static vars vs local vars Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "static vars vs local vars" Watch "static vars vs local vars" New topic

static vars vs local vars


Joined: Jan 08, 2002
Posts: 13
hi all,

The first class uses static variable :

and the second class uses local vars

I have run the test and the local variable version run 10 times faster than the version using static var.

Could you pls explain why the local variable version runs faster? I thought that the static must run faster because the local variables are freed after method call but static variables are not


Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
I do not think that you are comparing static variables with local variables. You are comparing inline integer literals (e.g. 10) with constants (static final int B10 = 10 ; ) .

I am surprised that there is a big difference in performance, as I would have thought that the hotspot compiler would manage to inline the constants.
[ October 14, 2004: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

which jvm (1.1, 1.2?), which flags (-sever y/n?).

jvms: 1.4.2, 1.5.0
-server y/n
-MAX 100,000 and 10,000,000
no significant differences measured.

I'm glad to have a benchmark template, to check all those observations myself, which avoids useless tuning-ideas.

Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Peter Chase:
I would have thought that the hotspot compiler would manage to inline the constants.

I think it might even already be inlined at compile time.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Terry McKee
Ranch Hand

Joined: Sep 29, 2000
Posts: 173
I tried the same examples several times and ended up with no visible difference in time execution when running both the examples. I am running v1.4.2_04
Terry McKee
Ranch Hand

Joined: Sep 29, 2000
Posts: 173
The performance difference that you see is probably because it takes longer to execute a program using the JRE initially. There may be stuff going on behind the scenes the first time you access the JRE while your program is running. I would try running both the programs several times one right after each other to get a more accurate picture of what is truly happening.

Joined: Jan 08, 2002
Posts: 13
Dear all,

Sorry for making a mistake in posting my message. I tested the application with Oracle 10g and 9i Java Virtual Machine by loading these class into Oracle database and run, to my suprise the version with static vars run 10 slower.

Here is the step to test:
1. compile java files

2. use loadjava -u db_username/db_password AESR.class to load it into
Oracle database
3. log in Oracle with the same database user above. Use this code to declare the function

4. Run the funtion with "set timing on" and "select testR from dual;"

Run this normal version with the same step as above

SQL code

As we have tested these versions run nearly the same if it runs with JDK1.4 on Linux, but I don't know why it is too different in Oracle VM.
Have anyone got experience in profiling Oracle VM?

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

Well, I'll tell you, friend. I just looked a bit more closely at these two programs, and I noticed that the first one's got one more zero in that right big old number in the "for" loop. Not rightly sure what that big old number does, but I hear tell it has some kind of effect on the running time of a program. Now, adding one more zero might not seem like much of a change, being just a zero and all, but maybe, just maybe, that might be your explanation right there.

[Jess in Action][AskingGoodQuestions]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

efh - observer of the week!
I agree. Here's the link: http://aspose.com/file-tools
subject: static vars vs local vars
Similar Threads
Java nio
June Newsletter Puzzle
Enumeration vs Iterator
Challenge: Optimum way to randomize 1,000,000 numbers
One important performance related question