Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6184257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:30:20+00:00 2026-05-24T01:30:20+00:00

I was creating a faster string splitter method. First, I wrote a non-tail recursive

  • 0

I was creating a faster string splitter method. First, I wrote a non-tail recursive version returning List. Next, a tail recursive one using ListBuffer and then calling toList (+= and toList are O(1)). I fully expected the tail recursive version to be faster, but that is not the case.

Can anyone explain why?

Original version:

def split(s: String, c: Char, i: Int = 0): List[String] = if (i < 0) Nil else {
  val p = s indexOf (c, i)
  if (p < 0) s.substring(i) :: Nil else s.substring(i, p) :: split(s, c, p + 1)
}

Tail recursive one:

import scala.annotation.tailrec
import scala.collection.mutable.ListBuffer
def split(s: String, c: Char): Seq[String] = {
  val buffer = ListBuffer.empty[String]
  @tailrec def recurse(i: Int): Seq[String] =  {
    val p = s indexOf (c, i)
    if (p < 0) {
      buffer += s.substring(i)
      buffer.toList
    } else {
      buffer += s.substring(i, p)
      recurse(p + 1)
    }
  }
  recurse(0)
}

This was benchmarked with code here, with results here, by #scala’s jyxent.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:30:21+00:00Added an answer on May 24, 2026 at 1:30 am

    You expect the tail recursive version to be faster due to the tail call optimization and I think this is right, if you compare apples to apples:

    def split3(s: String, c: Char): Seq[String] = {
      @tailrec def recurse(i: Int, acc: List[String] = Nil): Seq[String] =  {
        val p = s indexOf (c, i)
        if (p < 0) {
          s.substring(i) :: acc
        } else {
          recurse(p + 1, s.substring(i, p) :: acc)
        }
      }
      recurse(0) // would need to reverse
    }
    

    I timed this split3 to be faster, except of course to get the same result it would need to reverse the result.

    It does seem ListBuffer introduces inefficiencies that the tail recursion optimization cannot make up for.

    Edit: thinking about avoiding the reverse…

    def split3(s: String, c: Char): Seq[String] = {
      @tailrec def recurse(i: Int, acc: List[String] = Nil): Seq[String] =  {
        val p = s lastIndexOf (c, i)
        if (p < 0) {
          s.substring(0, i + 1) :: acc
        } else {
          recurse(p - 1, s.substring(p + 1, i + 1) :: acc)
        }
      }
      recurse(s.length - 1)
    }
    

    This has the tail call optimization and avoids ListBuffer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which one of these 2 cases is faster in creating files? Case 1: ======
Are the old tricks (lookup table, approx functions) for creating faster implementations of sqrt()
Is creating an index for a column that is being summed is faster than
Having used Java for a long time my standard method for creating long strings
Which option is better and faster? Insertion of data after creating index on empty
I am creating a database table that'll have a list of all Tags available
I'm creating a tic-tac-toe game, and one of the functions has to iterate through
I am creating a simple jQuery object by passing in some HTML string. In
I noticed that disposing and re-creating a DataContext is way faster than refreshing it
I am creating a moving text display with a huge font size. The faster

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.