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 6134761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:23:46+00:00 2026-05-23T17:23:46+00:00

I am comparing the performance of two atoi implementations. The first is iterating on

  • 0

I am comparing the performance of two atoi implementations. The first is iterating on the input string getting chars using charAt; the second is using foldLeft.

object Atoi {
  def withRandomAccess(str: String, baze: Int): Int = {
      def process(acc: Int, place: Int, str: String, index: Int): Int = 
        if (index >= 0) process(acc + value(str.charAt(index)) * place, place * baze, str, index-1) else acc
      process(0, 1, str, str.length - 1)
    }

  def withFoldLeft(str: String, base: Int): Int = (0/:str) (_ * base + value(_))

  def value(c: Char): Int = { /* omitted for clarity */ }

  def symbol(i: Int): Char = { /* omitted for clarity */ }
}

The foldLeft version is 2x to 4x slower (the complete benchmark code is here). I didn’t expect this. Do you know why? Is Scala converting the string to a List before processing it? Do you have a hint on how to boost foldLeft performance on strings?

  • 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-23T17:23:46+00:00Added an answer on May 23, 2026 at 5:23 pm

    The issue is nothing to do with inlining, it is to do with the boxing/unboxing of Chars that is taking place when you use the foldLeft.

    You get foldLeft on String by an implicit conversion to a StringOps, which is not specialized. Each char in the String has to be boxed into a java.lang.Character in order to be passed into the Function2 (argument to foldLeft), then unboxed (much cheaper) to be passed into the value method within the function body, then boxed again to be fed into the next iteration of the fold.

    The boxing involves the overhead of creating objects and subsequently garbage-collecting them.


    In terms of avoiding boxing, there follows a brief and important point:

    • you shouldn’t attempt to avoid boxing, with a probability of almost 1.

    (That is to say, unless you have identified a specific and unacceptable performance degradation which can be attributed to boxing, then you should not worry about it.)

    If you are sure that there is an issue which you need to address, avoid the collections and for-comprehensions (which use foreach and flatMap under hood). If you are using a loop, use while.

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

Sidebar

Related Questions

Comparing the two data models in an asp.net MVC app, which provides better performance,
Comparing string in C# is pretty simple. In fact there are several ways to
When comparing two strings in c# for equality, what is the difference between InvariantCulture
When comparing two objects (of the same type), it makes sense to have a
In C#, would there be any difference in performance when comparing the following THREE
What would anyone consider the most efficient way to merge two datasets using Python?
Performance wise, is there really a big difference between using: ArrayList.contains(o) vs foreach|iterator LinkedList.contains(o)
Comparing two codes below,both do the same,but with slighty differences: ALTER procedure [dbo].[SP_USUARIOS_UPDATE] @usu_ds
When comparing the performance of operations this is how I would typicaly do the
I'm currently comparing the performance of 4 different Oracle .net drivers. ODP.Net, DataDirect, OraDirect

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.