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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:57:56+00:00 2026-06-02T07:57:56+00:00

In my thinking, clojure vectors have a slight performance hit compared to java arrays.

  • 0

In my thinking, clojure vectors have a slight performance hit compared to java arrays. As a result I thought that “conventional wisdom” was that for those performance-critical parts of your code, you’d be better off using java arrays.

My tests however suggest that this is not true:

Clojure 1.3.0
user=> (def x (vec (range 100000)))
#'user/x
user=> (def xa (int-array x))
#'user/xa
user=> (time  (loop [i 0 s 0] (if (< i 100000) (recur (inc i) (+ s (nth x i))) s)))
"Elapsed time: 16.551 msecs"
4999950000
user=> (time  (loop [i 0 s 0] (if (< i 100000) (recur (inc i) (+ s (aget xa i))) s)))
"Elapsed time: 1271.804 msecs"
4999950000

As you can see, the aget adds about 800% time to this addition. Both methods still are way slower than native java though:

public class Test {                                                                                                                                                                                                                                                                                                           
    public static void main (String[] args) {                                                                                                                                                                                                                                                                                 
        int[] x = new int[100000];                                                                                                                                                                                                                                                                                            
        for (int i=0;i<100000;i++) {                                                                                                                                                                                                                                                                                          
            x[i]=i;                                                                                                                                                                                                                                                                                                           
        }                                                                                                                                                                                                                                                                                                                     
        long s=0;                                                                                                                                                                                                                                                                                                             
        long end, start = System.nanoTime();                                                                                                                                                                                                                                                                                  
        for (int i=0;i<100000;i++) {                                                                                                                                                                                                                                                                                          
            s+= x[i];                                                                                                                                                                                                                                                                                                         
        }                                                                                                                                                                                                                                                                                                                     
        end = System.nanoTime();                                                                                                                                                                                                                                                                                              
        System.out.println((end-start)/1000000.0+" ms");                                                                                                                                                                                                                                                                      
        System.out.println(s);                                                                                                                                                                                                                                                                                                
    }                                                                                                                                                                                                                                                                                                                         
}                              

> java Test
1.884 ms
4999950000

So, should my conclusion be that aget is 80 times slower than nth, and about 800 times slower than []-access in java?

  • 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-02T07:57:58+00:00Added an answer on June 2, 2026 at 7:57 am

    I suspect this is down to reflection and autoboxing of the primitive types by the aget function….

    Luckily aget/aset have performant overloads for primitive arrays that avoid the reflection and just do a direct array[i] access (See here and here).

    You just need to pass a type hint to pick up the right function.

    (type xa)
    [I    ; indicates array of primitive ints
    
    ; with type hint on array
    ;
    (time (loop [i 0 s 0] 
            (if (< i 100000) (recur (inc i) 
              (+ s (aget ^ints xa i))) s))) 
    "Elapsed time: 6.79 msecs"
    4999950000
    
    ; without type hinting
    ;
    (time (loop [i 0 s 0] 
            (if (< i 100000) (recur (inc i) 
              (+ s (aget xa i))) s)))
    "Elapsed time: 1135.097 msecs"
    4999950000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am thinking about learning Clojure, but coming from the c-syntax based (java, php,
Thinking about avoiding code replication, I got a question that catches me every time
Thinking that the answer to this is pretty obvious but here it goes: When
Just thinking about the best way to build an Order form that would (from
Am I right in thinking that until I am able to afford dedicated servers
I have asked this question on twitter as well the #clojure IRC channel, yet
Here is the problem: I have a very complex plugin that does lots of
Thinking sphinx documentation says that it automatically sorts the results based on relevance. What
I have a few questions concerning Lists, classes and variables in clojure. This may
Thinking about a Windows-hosted build process that will periodically drop files to disk to

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.