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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:05:25+00:00 2026-05-28T08:05:25+00:00

I wrote a binary search function as part of a larger program, but it

  • 0

I wrote a binary search function as part of a larger program, but it seems to be slower than it should be and profiling shows a lot of calls to methods in clojure.lang.Numbers.

My understanding is that Clojure can use primitives when it can determine that it can do so. The calls to the methods in clojure.lang.Numbers seems to indicate that it’s not using primitives here.

If I coerce the loop variables to ints, it properly complains that the recur arguments are not primitive. If i coerce those too, the code works again but again it’s slow. My only guess is that (quot (+ low-idx high-idx) 2) is not producing a primitive but I’m not sure where to go from here.

This is my first program in Clojure so feel free to let me know if there are more cleaner/functional/Clojure ways to do something.

(defn binary-search
  [coll coll-size target]
  (let [cnt (dec coll-size)]
    (loop [low-idx 0 high-idx cnt]
      (if (> low-idx high-idx)
        nil
        (let [mid-idx (quot (+ low-idx high-idx) 2) mid-val (coll mid-idx)]
          (cond
            (= mid-val target) mid-idx
            (< mid-val target) (recur (inc mid-idx) high-idx)
            (> mid-val target) (recur low-idx (dec mid-idx))
            ))))))

(defn binary-search-perf-test
  [test-size]
  (do
    (let [test-set (vec (range 1 (inc test-size))) test-set-size (count test-set)]
      (time (count (map #(binary-search2 test-set test-set-size %) test-set)))
    )))
  • 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-28T08:05:26+00:00Added an answer on May 28, 2026 at 8:05 am

    First of all, you can use the binary search implementation provided by java.util.Collections:

    (java.util.Collections/binarySearch [0 1 2 3] 2 compare)
    ; => 2
    

    If you skip the compare, the search will be faster still, unless the collection includes bigints, in which case it’ll break.

    As for your pure Clojure implementation, you can hint coll-size with ^long in the parameter vector — or maybe just ask for the vector’s size at the beginning of the function’s body (that’s a very fast, constant time operation), replace the (quot ... 2) call with (bit-shift-right ... 1) and use unchecked math for the index calculations. With some additional tweaks a binary search could be written as follows:

    (defn binary-search
      "Finds earliest occurrence of x in xs (a vector) using binary search."
      ([xs x]
         (loop [l 0 h (unchecked-dec (count xs))]
           (if (<= h (inc l))
             (cond
               (== x (xs l)) l
               (== x (xs h)) h
               :else nil)
             (let [m (unchecked-add l (bit-shift-right (unchecked-subtract h l) 1))]
               (if (< (xs m) x)
                 (recur (unchecked-inc m) h)
                 (recur l m)))))))
    

    This is still noticeably slower than the Java variant:

    (defn java-binsearch [xs x]
      (java.util.Collections/binarySearch xs x compare))
    

    binary-search as defined above seems to take about 25% more time than this java-binsearch.

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

Sidebar

Related Questions

I wrote this function that uses a binary search to look for a specific
I wrote the following program for conducting Binary search on an array when the
I am writing a test for a binary search function I wrote. module Tests
In an Open Source program I wrote , I'm reading binary data (written by
I wrote a program to test my binary tree and when I run it,
Is there a library function that performs binary search on a list/tuple and return
This is part of a search function on a website. So im trying to
I write a simple binary search program and meet a problem, see my three
I'm trying to write a wstring to file with ofstream in binary mode, but
I am trying to write a program that allows a binary to be run,

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.