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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T13:03:15+00:00 2026-06-18T13:03:15+00:00

This test for (;;) { int[] a = new int[10]; System.gc(); long t0 =

  • 0

This test

        for (;;) {
            int[] a = new int[10];
            System.gc();
            long t0 = System.currentTimeMillis();
            for (int i = 0; i < 1000000; i++) {
//              int[] b =  a.clone();
                int[] b =  Arrays.copyOf(a, a.length);
            }
            System.out.println(System.currentTimeMillis() - t0);
        }

shows ~50ms for Arrays.copyOf and ~160 ms for clone. Clone is a special native method for making copies, why is it so slow?

I ran the test on my HotSpot Client JVM 1.7.0_11-b21. Note that when the array increases in size, the difference between clone and copyOf disappears.

  • 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-18T13:03:16+00:00Added an answer on June 18, 2026 at 1:03 pm

    I’ve found a good article explaining why clone is slow here http://www.javaspecialists.eu/archive/Issue124.html. To put it shortly, it’s because int[].clone simply uses Object.clone and this method makes two checks before copying the array:

    1.Check whether instance is normal object or array.

    2.Check whether array is of primitives or objects.

    I added these checks to the Arrrays.copyOf test

            for (int i = 0; i < 1000000; i++) {
                Class cls = a.getClass();
                if (cls.isArray() && !cls.getComponentType().isAssignableFrom(Object.class)) {
                    int[] b = Arrays.copyOf(a, a.length);
                }
            }
    

    and the test showed no difference between clone and Arrays.copyOf. Had it been a specialized version for arrays clone would have been fast.

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

Sidebar

Related Questions

Exception in thread Thread-2 java.lang.NumberFormatException: For input string: 3 int test = Integer.parseInt(result[0]); This
Lets say i have this usercontrol public class test : UserControl { public int
say I have: class Test { public static int Hello = 5; } This
class Test { ; int x; }; Is this perfectly legal and portable?
I thought that this is the most efficient way to concatinate strings new StringBuilder(s1.length()
I am curious why this code works: typedef struct test_struct { int id; }
With this table: CREATE TABLE test_insert ( col1 INT, col2 VARCHAR(10), col3 DATE )
In this test Core Data project, I have a one to many relationship from
On this test page: http:// www.onebagoneearth.com/ Products-test , where it says oboe love series,
I have this test slider http://jsfiddle.net/dUyLY/2/ In Chrome it works nice, but in firefox

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.