@RunWith(AndroidJUnit4::class)
class StringConcatenationBenchmark {
private val context: Context = ApplicationProvider.getApplicationContext()
private val timeString = "5 минут"
@get:Rule
val benchmarkRule = BenchmarkRule()
@Test
fun buildStringWithAppend() {
benchmarkRule.measureRepeated {
val result = buildString {
append(timeString)
append(' ')
append(context.getString(R.string.ago)) // "назад"
}
// Чтобы компилятор не выкинул результат как неиспользуемый
result.toString()
}
}
@Test
fun stringFormatWithResource() {
benchmarkRule.measureRepeated {
val result = context.getString(R.string.ago_formatted, timeString) // "%s назад"
result.toString()
}
}
}