By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,470 Members | 1,907 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,470 IT Pros & Developers. It's quick & easy.

performance question

P: n/a
how well is javac from sun's jdk tools optimized?

wow, that's a general question... so i guess here is a simple example...
let's say i have a program that uses an array.
and many times in the program i need to do computation that rely on the
array's length.

so, how much speedup (if any at all) is there to storing the array length
as an int and using that in the computations, compared with constantly
using the arrayName.length method?

thanks for any insight, or pointing me to some not-too-in-depth
documentation on the runtime system of java and how optimizations at
compile time are done.

thanks again!

murat

--
Murat Tasan
mx**@po.cwru.edu
ta***@eecs.cwru.edu
mu*********@cwru.edu
http://genomics.cwru.edu

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Murat Tasan <ta***@eecs.cwru.edu> wrote in message news:<Pine.SOL.4.53.0310231524410.28593@homer>...
how well is javac from sun's jdk tools optimized?

wow, that's a general question... so i guess here is a simple example...
let's say i have a program that uses an array.
and many times in the program i need to do computation that rely on the
array's length.

so, how much speedup (if any at all) is there to storing the array length
as an int and using that in the computations, compared with constantly
using the arrayName.length method?


In this case, you wouldn't see any speed improvement, because length
isn't a method; it's an instance variable.

As Donald Knuth wrote, "Premature optimization is the root of all
evil." You should write your code in the most natural way. Afterwards,
if you are not satisfied with performance, use a profiler to find out
where the bottlenecks are and just optimize the parts that need it.
Jul 17 '05 #2

P: n/a
Murat Tasan <ta***@eecs.cwru.edu> wrote in message news:<Pine.SOL.4.53.0310231524410.28593@homer>...
let's say i have a program that uses an array.
and many times in the program i need to do computation that rely on the
array's length.

so, how much speedup (if any at all) is there to storing the array length
as an int and using that in the computations, compared with constantly
using the arrayName.length method?
murat


hi
in your example, your method has following disadvantages
1) you must update length variable as and when array is allocated new
elements
for eg
if in your prog you have something like arr=new int[5]; then you must
update length variable maintained by you.
2) its a duplicate variable ... i mean arr.length is a variable & not
a method, so why you want to maintain a copy of original variable
3) if you have 10 arrays , do you want to allocate 10 variables for
maintaining array size when its already taken care of ....
or would you go for havin an array of size 10 say lengths[10] ;) &
then maintain size of this array in seperate variable ;)

hope i made it clear
regards
amey
Jul 17 '05 #3

P: n/a
compared to what? GCC? not very

"Murat Tasan" <ta***@eecs.cwru.edu> wrote in message
news:Pine.SOL.4.53.0310231524410.28593@homer...
how well is javac from sun's jdk tools optimized?

wow, that's a general question... so i guess here is a simple example...
let's say i have a program that uses an array.
and many times in the program i need to do computation that rely on the
array's length.

so, how much speedup (if any at all) is there to storing the array length
as an int and using that in the computations, compared with constantly
using the arrayName.length method?

thanks for any insight, or pointing me to some not-too-in-depth
documentation on the runtime system of java and how optimizations at
compile time are done.

thanks again!

murat

--
Murat Tasan
mx**@po.cwru.edu
ta***@eecs.cwru.edu
mu*********@cwru.edu
http://genomics.cwru.edu

Jul 17 '05 #4

P: n/a

"Karl von Laudermann" <ka**@ueidaq.com> wrote in message
news:e7**************************@posting.google.c om...
Murat Tasan <ta***@eecs.cwru.edu> wrote in message

news:<Pine.SOL.4.53.0310231524410.28593@homer>...
how well is javac from sun's jdk tools optimized?

wow, that's a general question... so i guess here is a simple example...
let's say i have a program that uses an array.
and many times in the program i need to do computation that rely on the
array's length.

so, how much speedup (if any at all) is there to storing the array length as an int and using that in the computations, compared with constantly
using the arrayName.length method?


In this case, you wouldn't see any speed improvement, because length
isn't a method; it's an instance variable.

As Donald Knuth wrote, "Premature optimization is the root of all
evil." You should write your code in the most natural way. Afterwards,
if you are not satisfied with performance, use a profiler to find out
where the bottlenecks are and just optimize the parts that need it.


It is amazing how much work modern compilers do in optimisation. Even if you
did make a method call at each loop, it would cost you bugger all in running
time. The compiler will even clear up programmer stupidity, such as creating
variables that aren't used. I'd have to look up the specifics of javac to
tell you exactly what it does though. My favourite optimisation task (after
manual profiling) is simplifying, sorting and predicting the size of data
structures, which can make significant savings in complexity. I agree with
the post above (and Knuth), but with the preserve that it is not a license
for lazy design programming (always using (ie) util.Vector and never
bothering to sort anything).

John
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.