Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Performance Considerations in a Program  RSS feed

 
Vaibhav Gargs
Ranch Hand
Posts: 105
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please share your experiences for performance considerations in a Java program. What are the good/bad practices in general?
 
Campbell Ritchie
Marshal
Posts: 55747
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's duplicate this discussion in our Performance forum.
  • 1: Forget about performance. Only worry about performance if somebody complains the program is running slowly.
  • 2: Write straightforward code. Plain simple code which can easily be read and maintained. Forget about performance. Maintenance is much more important. Then search for the conversation with Brian Goetz where he tells you to write dumb code.
  • 3: Use efficient algorithms: you need to know why bubble sort is inefficient and under which circumstances bubble sort will run faster than merge sort.
  • 4: Use the standard techniques to avoid memory leaks and resource leaks.
  • 5: Don't try to guess where your performance bottlenecks are. Unless you have measured the performances of single blocks, your guess will have a high chance of being incorrect and you will make things worse by optmising the wrong part of the code. There are profilers which will look for bottlenecks for you: be sure to use them.
  •  
    Vaibhav Gargs
    Ranch Hand
    Posts: 105
    2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Campbell!

    Can you please elaborate and give some examples for Point 4.
     
    Campbell Ritchie
    Marshal
    Posts: 55747
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Start with try with resources.
    Whenever you add something to a List or array, consider whether it will be needed as long as the lifetime of the data structure. Joshua Bloch in Effective Java explains that you would have to remove an element from the backing data structure when popping something from a stack implementation.
    Consider weak references and similar.
    As you see, there aren't that many instances where you are at risk of leaks.
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12542
    48
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I would also elaborate a bit on #1. Just because someone says it runs slowly doesn't mean you should change your code.  You should have a documented reason and expectation before making changes. A user may complain if it takes more than a second to get a response, but if they spend a minute entering data on a form, is it worth the time and money to speed things up?  Is it worth the added cost of maintaining the more complex code to save a user 30 seconds out of an hour?

    Now, if you are programming a missle guidance system, where a delay can cause it to land 2 miles off course, then yes, you have real and legitimate time constraints. If you are dosing a cancer patient with radiation, then yes, you want to be sure you turn the beam off after the exact right number of miliseconds (micro? i don't know...).

    Another consideration...often it's not improvaing the program that is the best use of your resource.  a faster CPU, more memory, or a bigger network pipe may provide a better solution at a cheaper price than writing more complex code.
     
    mohammad shadab
    Ranch Hand
    Posts: 62
    Eclipse IDE Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Just think will program will execute on its best .
    same output can be produced in 10 minutes by writing dirty code . And same output can be produced in 5 minutes using good code.

    consider your memory consumption
    consider time it takes to execute
    consider any blackhole which can make your program to run for long time or for infinite time (example infinite loops)
    know number of user will use program, let say you have 100 MB mory to run program and your program reads some data thru buffers and yiu set buffer size 5 MB so as soon as 20 user start using same program (20*5=100) you will run out of memory and program will halt, so know numver of user and custimize your things.
    consider if you are i tracting with database , database has optimized queries.
    consider your program does not run to fetch un necessary data from db.
    consider you database not loaded with hell number of queries, try to group quries.
    consider you do use connevtion pooling for db .

    If you can tell which type of program you are talking about  then there is alot to speak.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!