https://kotlinlang.org logo
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
benchmarks
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
confetti
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
lincheck
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by
Title
j

james

02/14/2022, 10:38 PM
in Kotlin, what is the most efficient way to replace many strings inside another string? what I mean is, I could do:
originalString
    .replace("cat", "dog")
    .replace("goat", "cow")
    .replace("horse", "pig")
..but chaining those
replace()
calls would become very inefficient I assume, even if I created a map to iterate over. does Kotlin have any functions I can use to do a single pass over
originalString
and replace X number of items?
e

ephemient

02/14/2022, 10:43 PM
originalString.replace("cat|goat|horse".toRegex()) {
    when (val value = it.value) {
        "cat" -> "dog"
        "goat" -> "cow"
        "horse" -> "pig"
        else -> value
    }
}
😮 3
👍 1
j

james

02/14/2022, 11:28 PM
nice one, thanks mate!
a

Alexander Maryanovsky

02/15/2022, 5:42 AM
Note that the overhead of a regular expression is very high. Unless you're manipulating a very big string, this is going to be slower than just a chained
replace
.
e

ephemient

02/15/2022, 7:49 AM
that's true, Regex (which just wraps java.util.regex.Pattern on JVM) doesn't optimize this case, and incurs quite a bit of overhead. but if you have to take care with overlapping matches, it can be a bit challenging to handle with a loop of string replacements. this is something where an algorithm like https://github.com/robert-bor/aho-corasick can do far better than the naive approach at scale. running the following benchmark on my machine, I get the results
Benchmark                                       (replacements)  (size)   Mode  Cnt        Score        Error  Units
StringReplaceBenchmark.replaceMultiple                   Small      10  thrpt    5  6843692.841 ± 403750.822  ops/s
StringReplaceBenchmark.replaceMultiple                   Small     100  thrpt    5   752203.152 ±  24401.086  ops/s
StringReplaceBenchmark.replaceMultiple                   Small    1000  thrpt    5    56490.448 ±    833.514  ops/s
StringReplaceBenchmark.replaceMultiple                  Medium      10  thrpt    5  3439929.575 ±  37718.221  ops/s
StringReplaceBenchmark.replaceMultiple                  Medium     100  thrpt    5   316184.481 ±   5520.951  ops/s
StringReplaceBenchmark.replaceMultiple                  Medium    1000  thrpt    5    18601.717 ±   1656.362  ops/s
StringReplaceBenchmark.replaceMultiple                   Large      10  thrpt    5   493234.512 ±  14192.615  ops/s
StringReplaceBenchmark.replaceMultiple                   Large     100  thrpt    5    60454.752 ±   2081.056  ops/s
StringReplaceBenchmark.replaceMultiple                   Large    1000  thrpt    5     2916.083 ±     91.152  ops/s
StringReplaceBenchmark.replaceRegex                      Small      10  thrpt    5   640718.250 ±   6910.791  ops/s
StringReplaceBenchmark.replaceRegex                      Small     100  thrpt    5    75116.168 ±    506.125  ops/s
StringReplaceBenchmark.replaceRegex                      Small    1000  thrpt    5     8765.309 ±    684.884  ops/s
StringReplaceBenchmark.replaceRegex                     Medium      10  thrpt    5   194739.721 ±   2636.998  ops/s
StringReplaceBenchmark.replaceRegex                     Medium     100  thrpt    5    43549.693 ±    650.096  ops/s
StringReplaceBenchmark.replaceRegex                     Medium    1000  thrpt    5     2896.253 ±     23.372  ops/s
StringReplaceBenchmark.replaceRegex                      Large      10  thrpt    5    21717.137 ±    258.507  ops/s
StringReplaceBenchmark.replaceRegex                      Large     100  thrpt    5     3740.151 ±     90.825  ops/s
StringReplaceBenchmark.replaceRegex                      Large    1000  thrpt    5      395.084 ±     15.892  ops/s
StringReplaceBenchmark.replaceRegexPrecompiled           Small      10  thrpt    5  1152230.965 ±  36441.508  ops/s
StringReplaceBenchmark.replaceRegexPrecompiled           Small     100  thrpt    5    89396.970 ±   2498.650  ops/s
StringReplaceBenchmark.replaceRegexPrecompiled           Small    1000  thrpt    5     8850.670 ±     94.337  ops/s
StringReplaceBenchmark.replaceRegexPrecompiled          Medium      10  thrpt    5   297080.038 ±   8322.604  ops/s
StringReplaceBenchmark.replaceRegexPrecompiled          Medium     100  thrpt    5    47088.269 ±   1587.499  ops/s
StringReplaceBenchmark.replaceRegexPrecompiled          Medium    1000  thrpt    5     2940.105 ±     50.378  ops/s
StringReplaceBenchmark.replaceRegexPrecompiled           Large      10  thrpt    5    34627.292 ±   1444.838  ops/s
StringReplaceBenchmark.replaceRegexPrecompiled           Large     100  thrpt    5     4001.184 ±    155.575  ops/s
StringReplaceBenchmark.replaceRegexPrecompiled           Large    1000  thrpt    5      408.340 ±     18.374  ops/s
StringReplaceBenchmark.replaceTrie                       Small      10  thrpt    5   475508.504 ±  27061.453  ops/s
StringReplaceBenchmark.replaceTrie                       Small     100  thrpt    5   101975.264 ±    771.657  ops/s
StringReplaceBenchmark.replaceTrie                       Small    1000  thrpt    5    10060.275 ±    166.347  ops/s
StringReplaceBenchmark.replaceTrie                      Medium      10  thrpt    5    89048.167 ±    573.991  ops/s
StringReplaceBenchmark.replaceTrie                      Medium     100  thrpt    5    43124.884 ±    290.183  ops/s
StringReplaceBenchmark.replaceTrie                      Medium    1000  thrpt    5     5730.146 ±    155.629  ops/s
StringReplaceBenchmark.replaceTrie                       Large      10  thrpt    5    10000.751 ±    356.272  ops/s
StringReplaceBenchmark.replaceTrie                       Large     100  thrpt    5     8499.478 ±    114.008  ops/s
StringReplaceBenchmark.replaceTrie                       Large    1000  thrpt    5     3197.324 ±     37.847  ops/s
StringReplaceBenchmark.replaceTriePrecompiled            Small      10  thrpt    5  1646352.116 ±  38731.483  ops/s
StringReplaceBenchmark.replaceTriePrecompiled            Small     100  thrpt    5   118071.785 ±   2940.900  ops/s
StringReplaceBenchmark.replaceTriePrecompiled            Small    1000  thrpt    5    10122.962 ±    320.628  ops/s
StringReplaceBenchmark.replaceTriePrecompiled           Medium      10  thrpt    5  1011767.809 ±  48454.186  ops/s
StringReplaceBenchmark.replaceTriePrecompiled           Medium     100  thrpt    5    76427.972 ±   1846.560  ops/s
StringReplaceBenchmark.replaceTriePrecompiled           Medium    1000  thrpt    5     6137.517 ±     83.122  ops/s
StringReplaceBenchmark.replaceTriePrecompiled            Large      10  thrpt    5   804546.386 ±  11122.620  ops/s
StringReplaceBenchmark.replaceTriePrecompiled            Large     100  thrpt    5    59592.308 ±   4187.163  ops/s
StringReplaceBenchmark.replaceTriePrecompiled            Large    1000  thrpt    5     4790.049 ±    384.746  ops/s
which shows some that string replace gets linearly worse (as expected) while Aho-Corasick grows much slower
r

Roukanken

02/15/2022, 9:39 AM
tbh, with an efficient kotlin/java library for regex, you would have basically Aho-Corasick there automatically 😄
e

ephemient

02/15/2022, 9:46 AM
yep! but it won't be hooked up to kotlin.text.Regex so you'll have to use separate types just like this example anyway
j

Joffrey

02/15/2022, 9:54 AM
TIL there is a kotlinx.benchmark multiplatform library that wraps JMH on JVM :mind-blown: thanks @ephemient
:nice: 1