I am reading a file line by line consisting of 4 million records where I do split and replace operations to be used further down the line. While doing the split and replace, I found that using toRegex() boosts the performance.
private fun parseRecord(record: String) { // this is the method that will be called for each line
val tokens = record.split(" ") //record will always contain 2 tokens
}
// pseudo-code for calling parseRecord() for each line in the file
for line in file:
parseRecord(line)
This takes approximately 30seconds to complete the processing. Whereas if I use below code, it completes in 20seconds.
private fun parseRecord(record: String) {
val tokens = record.split(" ".toRegex())
}
The above scenario applies for replace as well.
What is the reason behind the increase in the performance while using toRegex()? Also, is there any other efficient way to further more optimise this?
Thanks in advance for the help!