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

  • Home
  • SEARCH
  • 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 7921019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:25:54+00:00 2026-06-03T16:25:54+00:00

I noticed queue.ml tends to be rather slow due to its implementation based on

  • 0

I noticed queue.ml tends to be rather slow due to its implementation based on a linked list (with a mutable field)

Is it possible to build a queue with a faster structure, like an array for instance ?

Or is there a way to achieve better performances but still in a functional way ?

EDIT : I want to implement a publish-subscribe solution with tens of thousands enqueue and deliver operations in less than one millisecond (for a graphic interactive application), so I need something very fast and ocaml queue implementation didn’t fullfill my performance needs

  • 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-03T16:25:56+00:00Added an answer on June 3, 2026 at 4:25 pm

    It would be interesting to know why you think the linked-list based implementation is slow. This is the classic implementation of a queue. Insert and delete are about as fast as I can imagine (updating one or two reference fields). Note that I haven’t looked at the code you’re talking about.

    You can implement a ring buffer with an array. It might be faster for some things (like traversing all elements, say), but would have a maximum size (for the most straightforward implementation). There are also complications in initializing the array.

    Generally speaking, functional (immutable) implementations of data structures are a bit slower than the corresponding imperative (mutable) versions. There’s a very nice functional queue implementation that has great performance. The basic trick is to keep half the queue in forward order (for fast removal) and half in reversed order (for fast insertion). The extra pass for reversal introduces a small penalty, like I was saying.

    If you’re looking at memory (allocation and GC), the immutable approach will allocate the most, the classic method a medium amount, and the ring buffer will allocate very little. But a good FP implementation (like OCaml) makes it very fast to allocate memory, and it’s not too slow to reclaim it either if the objects are short-lived.

    Edit: For what it’s worth, I just ran an exceptionally crude timing test on my laptop (3.06 GHz Intel Core 2 Duo), and I can enqueue and dequeue a million values using the standard OCaml Queue module in 70 millisec. So then that’s about .7 msec to do 10,000 (if I got that right). It doesn’t seem fast enough to do what you want if your CPU is no faster than mine.

    Edit 2: I just hand wrote some code that assumes there’s a pre-existing reference field in your values that can be used to queue them up. This avoids doing any allocations in the queueing code, but is otherwise similar to the standard Queue module (though far less elegant). On the same laptop as above, a million queues and dequeues takes around 48 millisec. So it’s a little faster.

    Edit 3: Very likely you’ve got your answer by now, but I just ran a crude timing test using the pure queue implementation I outlined above, and I see around 18 millisec to do a million queues and dequeues. So it’s quite a bit faster. This is plausible, as OCaml is tuned for pure computation. Here is my code; you can check for any errors.

    type q = {
        head: int list;
        tail: int list;
    }
    
    let q_add q i =
        { q with tail = i :: q.tail }
    
    let q_take q =
        match q.head with
        | [] ->
            begin
            match List.rev q.tail with
            | [] -> raise Not_found
            | h :: t -> (h, { head = t; tail = [] })
            end
        | h :: t -> (h, { head = t; tail = q.tail })
    
    let main () =
        let q = q_add { head = []; tail = [] } 0
        in let now = Unix.gettimeofday ()
        in let rec go q i =
            if i > 1_000_000 then
                q
            else
                let (_, q') = q_take (q_add q i)
                in
                    go q' (i + 1)
        in let _ = go q 1
        in let duration = Unix.gettimeofday () -. now
        in
            Printf.printf "%f\n" duration
    
    let () = main ()
    

    Like I said, this is a crude test. The queue just alternates between 1 and 2 elements in length. I’m not sure the results would hold up in your environment. But it’s interesting.

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

Sidebar

Related Questions

I need a Max-Priority Queue data structure. Looking in Java's Priority Queue I noticed
Ive been working on my personal page which is based on drupal. Ive noticed
I noticed that you can call Queue.Synchronize to get a thread-safe queue object, but
I'm having a bit of problems understanding how or if its possible to share
Noticed this today when a patch was submitted with the following line: lblCompletionTime.Text =
I noticed the existence of Microsoft.VisualStudio.TestTools.UnitTesting.PriorityAttribute. From reading a little about it, it does
I noticed that various systems use various characters as the replacent for illegal ones
I noticed that on my Vista and XP machines, the automatic update is asking
I noticed that JavaScript's new Date() function is very smart in accepting dates in
I noticed a moment ago that my .gitconfig -file was public at my repo.

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.