Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static vars vs local vars

 
THUAN VN
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Thanks
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which jvm (1.1, 1.2?), which flags (-sever y/n?).

OS:Linux
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
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Terry McKee
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
THUAN VN
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks
Thuan
Thuan
Thuan
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


efh - observer of the week!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic