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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:47:32+00:00 2026-05-14T19:47:32+00:00

In one of my first attempts to create functional code, I ran into a

  • 0

In one of my first attempts to create functional code, I ran into a performance issue.

I started with a common task – multiply the elements of two arrays and sum up the results:

var first:Array[Float] ...
var second:Array[Float] ...    
var sum=0f; 
for (ix<-0 until first.length) 
    sum += first(ix) * second(ix);

Here is how I reformed the work:

sum = first.zip(second).map{ case (a,b) => a*b }.reduceLeft(_+_)

When I benchmarked the two approaches, the second method takes 40 times as long to complete!

Why does the second method take so much longer? How can I reform the work to be both speed efficient and use functional programming style?

  • 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-14T19:47:33+00:00Added an answer on May 14, 2026 at 7:47 pm

    The main reasons why these two examples are so different in speed are:

    • the faster one doesn’t use any generics, so it doesn’t face boxing/unboxing.
    • the faster one doesn’t create temporary collections and, thus, avoids extra memory copies.

    Let’s consider the slower one by parts. First:

    first.zip(second)
    

    That creates a new array, an array of Tuple2. It will copy all elements from both arrays into Tuple2 objects, and then copy a reference to each of these objects into a third array. Now, notice that Tuple2 is parameterized, so it can’t store Float directly. Instead, new instances of java.lang.Float are created for each number, the numbers are stored in them, and then a reference for each of them is stored into the Tuple2.

    map{ case (a,b) => a*b }
    

    Now a fourth array is created. To compute the values of these elements, it needs to read the reference to the tuple from the third array, read the reference to the java.lang.Float stored in them, read the numbers, multiply, create a new java.lang.Float to store the result, and then pass this reference back, which will be de-referenced again to be stored in the array (arrays are not type-erased).

    We are not finished, though. Here’s the next part:

    reduceLeft(_+_)
    

    That one is relatively harmless, except that it still do boxing/unboxing and java.lang.Float creation at iteration, since reduceLeft receives a Function2, which is parameterized.

    Scala 2.8 introduces a feature called specialization which will get rid of a lot of these boxing/unboxing. But let’s consider alternative faster versions. We could, for instance, do map and reduceLeft in a single step:

    sum = first.zip(second).foldLeft(0f) { case (a, (b, c)) => a + b * c }
    

    We could use view (Scala 2.8) or projection (Scala 2.7) to avoid creating intermediary collections altogether:

    sum = first.view.zip(second).map{ case (a,b) => a*b }.reduceLeft(_+_)
    

    This last one doesn’t save much, actually, so I think the non-strictness if being “lost” pretty fast (ie, one of these methods is strict even in a view). There’s also an alternative way of zipping that is non-strict (ie, avoids some intermediary results) by default:

    sum = (first,second).zipped.map{ case (a,b) => a*b }.reduceLeft(_+_)
    

    This gives much better result that the former. Better than the foldLeft one, though not by much. Unfortunately, we can’t combined zipped with foldLeft because the former doesn’t support the latter.

    The last one is the fastest I could get. Faster than that, only with specialization. Now, Function2 happens to be specialized, but for Int, Long and Double. The other primitives were left out, as specialization increases code size rather dramatically for each primitive. On my tests, though Double is actually taking longer. That might be a result of it being twice the size, or it might be something I’m doing wrong.

    So, in the end, the problem is a combination of factors, including producing intermediary copies of elements, and the way Java (JVM) handles primitives and generics. A similar code in Haskell using supercompilation would be equal to anything short of assembler. On the JVM, you have to be aware of the trade-offs and be prepared to optimize critical code.

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

Sidebar

Related Questions

I've installed PowerShell recently and one of the first things I started looking for
The first one is definitely something that works, but which one below is the
I'm using C++ .NET 2.0 I have 2 forms the first one is declared
I'm trying to add a space before every capital letter, except the first one.
Everything is an object was one of the first things I learned about Ruby,
When you take your first look at an Oracle database, one of the first
I'm a beginner with SQL and am working on one of my first databases.
First, let me use one sentence to let out some frustration: My god, developing
First, what does one call the ghost caption that appears in a text edit
I've written my first JQuery plugin and one of it's dependancies is an external

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.