• 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

Variable declaration vs method call in Java Classes

 
Ranch Hand
Posts: 42
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Emphasis is on increasing performance by avoiding null Check and using a boolean in place of every null check.

I have a condition where there is a need to check whether the variable(Collection of some Form variables) is null or not,as the variable is not mandatory everytime.
So i need to do null check everytime.

My Doubt is which one is better among these two code snippets



Or



I know i can use a single if for null check but here that is not the case.

Which one is better method call or temp variable (boolean for null check) lookup?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just for fun, do a search for "premature optimization is the root of all evil" -

Program for clarity, not some piddling little "optimization."

If you really want to dig in, write some example code, disassemble it and look at the Java op-codes - let us know what you find. Keep in mind that modern JVMs can do lots of optimization on the fly.

Bill
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If that really is the structure of your code (no else blocks), I'd write it as
 
Jack Numen
Ranch Hand
Posts: 42
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Stuart and William..
Thanks for your responses. Yes i agree with you both at some level, lets us say when we are inserting 10-50 records into the Database using our piece of code which has many such conditional checks and iterations. What if there are 100 more records to insert at the same instance checking and validating each item in the record.

I agree that JVM does optimization on the fly and have also answered some queries related to optimisation stuff. In My case with such a piece of code i can insert 10-50 records in less than 15secs but when it comes to 100 more records like this it will(and is) definetly take 2-3mins for such insertions.

Although 2-3ms is not a matter for 10 records but when multiple by 10 then there is difference.
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ivan Jozsef Balazs
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even more important than (micro-)optimalisation is that the code compiles: this portion does not.


if(someCondition && null != myCollection && myCollection.size()){

 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In My case with such a piece of code i can insert 10-50 records in less than 15secs but when it comes to 100 more records like this it will(and is) definetly take 2-3mins for such insertions.

Although 2-3ms is not a matter for 10 records but when multiple by 10 then there is difference.



Whoa! You honestly think your piddling logical operations take CPU time measured in milliseconds!

I would bet money you would have a hard time proving a microsecond difference in 1000 operations.

DB operations will always always always take way longer than the code that prepares for them. Thats just the nature of the beast.

You need to do some actual profiling - plenty of tools are available.

Bill
 
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

Jack Numen wrote:I agree that JVM does optimization on the fly and have also answered some queries related to optimisation stuff. In My case with such a piece of code i can insert 10-50 records in less than 15secs but when it comes to 100 more records like this it will(and is) definetly take 2-3mins for such insertions.


100 more? Like William, I think you plainly have no idea just how fast modern processors are.

If there were 100 million more records, AND I could prove that:
(a) the code is currently running too slowly.
(b) this particular optimization makes all the difference between running too slow and running acceptably fast.
I might consider optimizing.

But to do that, as he says, you need to profile.

My favorite quote about optimization (it used to be my signature):
"More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason — including blind stupidity."
— W.A. Wulf

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic