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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:33:15+00:00 2026-06-01T11:33:15+00:00

I am currently trying to build a program where I have a recursive function

  • 0

I am currently trying to build a program where I have a recursive function that for every loop appends one new element to the array it is building. I didn’t want to use the append function that many times, because my function is supposed to do a large number of loops, and I’ve come to learn from previous experiences that the append function in general takes a lot of time. I’ve tried to look everywhere for a function that simply adds one element to the tail of the array, but I’ve found nothing of such sort. So I was thinking I would ask here.

So my question is basically: “Is there a more effective way of adding one element to the back of an array than using append?”


Updated with a new question regarding the previous one

So I used a list instead, inserting each new element as a head, and reverted the list when the function was finished. This made the function about 70 times faster. But the problem remains as I have another function that does pretty much the same, which became about 4 times slower, increasing the overall time for my main function. The functions are very similar. The first function (the one that became much faster) produces ints, adding each new int to the list. The second function (the one that became much slower) produces an object, adding each new object to the list. Does anyone have an idea why one function became so much faster while the other one became so much slower?

  • 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-06-01T11:33:16+00:00Added an answer on June 1, 2026 at 11:33 am

    ResizeArray is a more efficient data structure for this task, for example:

    let filterRange predicate (i, j) =
        let results = ResizeArray(j-i+1)
        for k = i to j do
            if predicate k then results.Add(k)
        results.ToArray()
    

    ResizeArray module from F# PowerPack provides high-order functions to manipulate ResizeArray in a functional way. However, beware that these functions create new ResizeArray instances which make them less efficient than .NET methods.

    A purely functional alternative is to use a list as an accumulator, prepend elements to the accumulator, reverse it (if the order matters) and convert to Array in the end:

    let filterRange predicate (i, j) =
        let rec loop k acc =
            if k = j+1 then acc
            elif predicate k then loop (k+1) (k::acc)
            else loop (k+1) acc
        loop i [] |> List.rev |> Array.ofList
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to build an application that inherently needs good time synchronization across
I am trying to build an API that gives data on various cities. Currently,
I'm trying to build firefox but I'm having some problems. I currently have Visual
I am trying to build a data processing program. Currently I use a double
I'm currently trying to get a program to compile on a system that I
I am currently trying to develop a small simulation program in xna that takes
I am very new to assembly, and I'm trying to build a small program.
I am currently trying to build a small system that read's in a bunch
Summary I currently have a NAnt build script that performs a vssget on either
I'm currently trying to build a personal website to create a presence on the

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.