https://kotlinlang.org logo
Title
s

Smallville7123

04/18/2019, 5:40 PM
assuming this is correct, could this be optimized? https://pl.kotl.in/BKCVGjs_8
j

jw

04/18/2019, 5:42 PM
pre-size string builder to known final length
s

Smallville7123

04/18/2019, 5:43 PM
what do you mean by that
j

jw

04/18/2019, 5:45 PM
if the string is 10 characters long and you're padding out to 40 characters, it'd be best to pre-size the string builder to 40, append string, append spaces, and call toString(). This ensures the backing character array never has to expand to accommodate the padding
k

karelpeeters

04/18/2019, 5:47 PM
Using the
ensureCapacity
you asked about earlier
nvm, there's a constructor parameter too of course.
s

Smallville7123

04/18/2019, 5:50 PM
like this?
fun String.padExtendEnd(to: Int, char: Char) = this.toStringBuilder().append(char.toString().repeat(to-this.length)).toString()
k

karelpeeters

04/18/2019, 5:52 PM
Like this:
import kotlin.math.max

fun String.padExtendEnd(to: Int): String {
    val build = StringBuilder(max(this.length, to));
    build.append(this);
    while(build.length != to) build.append(' ')
    return build.toString()
}
Also appending chars should be faster, at least it can't hurt.
s

Smallville7123

04/18/2019, 6:00 PM
hmmm ok
is
.repeat
slow?
k

karelpeeters

04/18/2019, 6:01 PM
Well it creates another
String
instance, which is dumb since you already have a new temporary array in the
StringBuilder
.
s

Smallville7123

04/18/2019, 6:02 PM
ok, the capacity thing is only available on Kotlin/Native right?
k

karelpeeters

04/18/2019, 6:02 PM
No.
s

Smallville7123

04/18/2019, 6:02 PM
oh
k

karelpeeters

04/18/2019, 6:03 PM
I think that's just an oversight, they should probably add it to the Kotlin stdlib.
s

Smallville7123

04/18/2019, 6:04 PM
is StringBuilder(this) faster than StringBuilder().append(this)
j

jw

04/18/2019, 6:09 PM
depends on the length of
this
and whether it causes expansion
the constructor will pre-allocate the length + 16
s

Smallville7123

04/18/2019, 6:12 PM
@jw so in which cases are each faster than the other?
j

jw

04/18/2019, 6:13 PM
if the string is less than 16 characters they are equally as fast but the former allocates more than the latter. if its more than 16 characters the former is faster and the latter allocates more
the fastest solution and the one which allocates the least is as i said above, create with pre-allocated exact size, append string, append whitespace, toString
k

karelpeeters

04/18/2019, 6:18 PM
Interestingly enough the stdlib does't do the
max
thing, I wonder if I'm wrong with that?
s

Smallville7123

04/18/2019, 6:26 PM
ok
does this make sense?
padExtendStart(1, ' ') = "a  "  4 expect " abc"
padExtendEnd  (1, ' ') = "a  "  4 expect "abc "
k

karelpeeters

04/18/2019, 6:34 PM
Your question doesn't make sense, no.
s

Smallville7123

04/18/2019, 6:37 PM
if
padExtendEnd
extends the end to
to
when it is larger then
this.length
specifying a value lower than
this.length
should do the opposite (in which as pads from the end
right
untill
to
characters remain) right? if so should the same apply for
padExtendStart
but in the other direction?
k

karelpeeters

04/18/2019, 6:38 PM
Usually
pad
functions don't work like this, eg. the ones in Kotlin don't.
s

Smallville7123

04/18/2019, 6:41 PM
oh
how else can it be optimized
also the
padExtend
and
padShrink
are based loosely on Sign Extension https://en.wikipedia.org/wiki/Sign_extension
k

karelpeeters

04/18/2019, 7:43 PM
I don't think it can be optimized any more.
s

Smallville7123

04/18/2019, 7:55 PM
ok