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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:45:08+00:00 2026-05-31T21:45:08+00:00

This is a clojure program to read integers from a file and count the

  • 0

This is a clojure program to read integers from a file and count the number of inversions i.e the number of times a larger number appears before a smaller number in a sequence. It implements a O(n^2) algorithm, and takes about 30 minutes to run on an input of size 100,000.

(defn count-inversions
  [file]
  (let [int-list (vec (map #(Integer/parseInt %)
                           (clojure.string/split (slurp file) #"\r\n")))]
    (loop [c 0
           t-list int-list]
      (let [size (count t-list)]
        (if (empty? t-list)
         c
         (recur (reduce #(if (< %2 (first t-list)) (inc %1) %1) c (subvec t-list 1 (dec  size)))
                (subvec t-list 1 (dec size))))))))

This algorithm when implemented in Java takes only a few seconds to complete. Why such a large difference?

  • 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-31T21:45:09+00:00Added an answer on May 31, 2026 at 9:45 pm

    Things that look potentially slow to me (though you’ll have to benchmark to be sure….)

    • All your numbers are boxed. This will make anything you do much slower than using primitives. It isn’t very idiomatic but you can use primitive arrays in Clojure if you want to get this speedup.
    • Reducing over a subvec currently creates a lot of temporary objects. There is a patch in the works to fix this (http://dev.clojure.org/jira/browse/CLJ-894) but you might have to wait for Clojure 1.4 or 1.5 for this.
    • It helps to do (set! *unchecked-math* true) to improve performance of integer operations (obviously, you need to be a bit more careful if overflows might happen)

    If I wanted to get this to run really fast in Clojure I’d probably resort to primitive arrays and looping with primitive indexes:

    (set! *unchecked-math* true)
    
    (defn count-inversions [coll]
         (let [^ints integers (int-array coll)
               size (long (count integers))]
           (loop [c (long 0)
                  i (long 0)]
             (if (>= i size)
               c
               (recur
                 (loop [c (long c)
                        v (aget integers i)
                        j (inc i)]
                   (if (>= j size)
                     c
                     (recur (long (if (> v (aget integers j)) (inc c) c)) v (inc j))))
                 (inc i))))))
    
    (time (count-inversions (for [i (range 100000)] (rand-int 100))))
    "Elapsed time: 16036.651863 msecs"
    

    i.e. this code counts all the inversions in 100,000 integers in about 16 seconds on my machine (with Clojure 1.3)

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

Sidebar

Related Questions

I am writing my first clojure program, and want to read lines from stdin.
I'd like to call a Clojure function from Java code. This question hasn't been
After a few weekends exploring Clojure I came up with this program. It allows
I'm writing a clojure program which parses XML. As a part of this, I
I am writing a function for my Clojure program that reads user input from
I have some printlns I need to capture from a Clojure program and I
I have a rather massive number of threads being created inside a clojure program:
I typed this into Clojure REPL (using the enclojure Netbeans plugin): user=> hello, world
I am trying to port this algorithm to clojure. My code is (defn calc-iterations
A friend gave me this code snippet in Clojure (defn sum [coll acc] (if

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.