Deciding Between String Concatenation and Template

Hi, I understand that string templates take less time to type down, but what about efficiency as far as execution goes? I wanna learn writing the most non-demanding apps possible.

The efficiency/optimization you mention here, is so small, its significantly negligible.

Sometimes readability, maintainability and ease of use far outweigh the performance gain. Squeezing every drop of performance will very likely mean you never finish the project. Or the code becomes so difficult to understand, other people would need ages to get up to speed with the project/code base.

2 Likes

OK, thanks, and which option is more efficient here?

If you have to ask that question, you missed the essence of my answer

The essence of your answer was dodging the question. I asked, you didn’t answer. I suspect you just don’t know. But thanks anyway.

As @stetim94 said you shouldn’t go down the path of what is most efficient for something like this. The answer is completely negligible, if you want to make efficient code you are best making it algorithmically efficient, the implementation at this level has almost no impact.

If you’re really keen to write super efficient code I wouldn’t recommend Kotlin, I’d use pure C.

If you still really want an answer to your question Templates are faster. Templates get compiled to a StringBuilder which just use an internal buffer and make one string object at the end. String concatenation creates a new string in the String pool with every single +

4 Likes

Purely out of curiosity, whilst I can’t imagine there’s an appreciable difference between the two, I decided to spin up a REPL and test it…

Here’s the horrible “quick-and-dirty” Kotlin program I made to test the two methods:

import kotlin.system.measureTimeMillis

fun main(args: Array<String>) {
  println("Kotlin - 'Efficient String Theory'")

  val timeConcatenation: Long = measureTimeMillis(::stringConcatenationTest)
  val timeTemplate: Long = measureTimeMillis(::stringTemplateTest)

  println("1000000 iterations of simple string concatenation took $timeConcatenation milliseconds.")
  println("1000000 iterations of simple string templating took $timeTemplate milliseconds.")
}

fun stringConcatenationTest() {
  val str1: String = "concatenation"
  val str2: String = "quicker?"
  // do a simple string concatenation 1000000 times
  for (i in 1..1000000) {
    val joinedString: String = "This is a test to see whether string " + str1 + " is any " + str2
  }
}

fun stringTemplateTest() {
  val str1: String = "template"
  val str2: String = "quicker?"
  // build a string template 1000000 times
  for (i in 1..1000000) {
    val templateString: String = "This is a test to see whether string $str1 is any $str2"
  }
}

The output from my REPL?

> kotlin -classpath main.jar MainKt
Kotlin - 'Efficient String Theory'
1000000 iterations of simple string concatenation took 380 milliseconds.
1000000 iterations of simple string templating took 207 milliseconds.

Granted, the REPL isn’t exactly Fugaku but the difference between the two methods over a large number of iterations is 173 milliseconds…

Think you can, quite safely, unburden yourself of any concerns as to whether you’re unnecessarily holding up your program by picking one over the other.

3 Likes