DEV Community

Discussion on: Performance Benchmarking: String and String Builder

Collapse
 
kaleemniz profile image
Kaleem

Reading the invaluable responses here is the highlight - the highlights will be helpful if there is part two on this subject.

  • Use a Microbenchmarking framework like JMH to see realistic results.

  • StringBuilder has a more complex API than String, so it's worth identifying the pivot point "k" for which StringBuilder becomes faster than String, making it easy to decide whether to use String or StringBuilder.

  • Do not pass String and StringBuilder as method parameters; instead, create String and StringBuilder inside the test functions and at the and use toString() to return the response.