#Determining/profiling Java program performance

1 messages · Page 1 of 1 (latest)

gleaming prairie
#

Hello, I'm learning Java - after writing some code I've gotten into the habit of asking ChatGPT for improvements without telling me exactly what to change.

I then take each step one at a time and make a commit on Github for it, so I know what I changed and why. If I get stuck, ChatGPT can give me further pointers that I read up on without going straight to the answer.
One of the points I recently received was to re-order some conditionals in an else-if loop, as this apparently improves performance.

To that end, how can I best assess the performance of my code? At the moment, I am running my code through VS Code.

I guess my expected user-flow is:

  1. Run code
  2. Am told how long it took to execute etc.
  3. I make my changes.
  4. I compare the statistics to see what the improvement was.

Many thanks

old siloBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
Hello, I'm learning Java - after writing some code I've gotten into the habit of asking ChatGPT for improvements without telling me exactly what to change. I then take each step one at a time and make a commit on Github for it, so I know what I changed an

e. Now I want to dive into performance optimization. What are some best practices for determining and improving the performance of a Java program?

  1. Start by profiling your code using tools like VisualVM, JProfiler, or YourKit. These tools can help you identify bottlenecks and areas for improvement in your code.

  2. Focus on optimizing algorithms and data structures first. Use efficient algorithms and data structures to improve the overall performance of your program.

  3. Avoid unnecessary object creation and memory allocations. Reuse objects where possible and minimize garbage collection overhead.

  4. Use appropriate collections based on the requirements of your program (e.g., ArrayList vs LinkedList) to ensure optimal performance.

  5. Consider using parallelism and concurrency to leverage multiple CPU cores for improved performance.

  6. Optimize I/O operations by minimizing disk reads/writes and network calls.

  7. Profile your code again after making optimizations to measure the impact of your changes and identify any new bottlenecks.

  8. Keep in mind that premature optimization can lead to complex and hard-to-maintain code. Focus on optimizing critical sections of your code that have the most significant impact on performance.

By following these best practices, you can effectively determine and improve the performance of your Java program while maintaining clean and maintainable code.

languid silo
#

JMH

#

but tbf, anything you do now that makes the code less readable is bad

#

Just write it as simple and as clear as you can possibly do

gleaming prairie
#

Thank you for your response. I will look into JMH. I recognise that in the early stages I'm unlikely to need to be going back through and optimising code, especially when it is taking milliseconds to run - but wanted to make sure I understood any potential best practices and have some statistics as an example on my notes.

I'll see how I get on

languid silo
#

yeah and ESPECIALLY java and the JVM it runs on is slow to start up

#

Once it gets going it's usually fast enough 🙂

#

Methods get really optimized after x 1000 calls etc

true ocean
#

Also do not blindly trust gpt. If your in doubt ask in here aswell. If gpt was as smart as it claimed it it, we'd all have to run from terminators

granite rock
#

JMH is good for microbenchmarking. for profiling your app, use a profiler such as JMC (JMX Console or Flight Recorder), VisualVM, JProfiler, etc..

rain jay
#

Also remember that perfs can be quite random

#

And note that JMH is the only reliable tool to determinate perfs

#

And even by using JMH, things may not be what they appear

#

And whatever someone says about X faster than Y, ask why it is the case, and test it

gleaming prairie
#

Ok, thanks all for the input. Found exactly what I was looking for and ended up watching a very interesting talk about JMH