Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Why is processing a sorted array faster than processing an unsorted array?

  1. #1
    Junior Member
    Join Date
    Apr 2022
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why is processing a sorted array faster than processing an unsorted array?

    Here is a piece of C++ code that shows some very peculiar behavior. For some strange reason, sorting the data (before the timed region) miraculously makes the loop almost six times faster.

    #include <algorithm>
    #include <ctime>
    #include <iostream>
     
    int main()
    {
        // Generate data
        const unsigned arraySize = 32768;
        int data[arraySize];
     
        for (unsigned c = 0; c < arraySize; ++c)
            data[c] = std::rand() % 256;
     
        // !!! With this, the next loop runs faster.
        std::sort(data, data + arraySize);
     
        // Test
        clock_t start = clock();
        long long sum = 0;
        for (unsigned i = 0; i < 100000; ++i)
        {
            for (unsigned c = 0; c < arraySize; ++c)
            {   // Primary loop
                if (data[c] >= 128)
                    sum += data[c];
            }
        }
     
        double elapsedTime = static_cast<double>(clock()-start) / CLOCKS_PER_SEC;
     
        std::cout << elapsedTime << '\n';
        std::cout << "sum = " << sum << '\n';
    }


    Without std::sort(data, data + arraySize);, the code runs in 11.54 seconds.

    With the sorted data, the code runs in 1.93 seconds.

    (Sorting itself takes more time than this one pass over the array, so it's not actually worth doing if we needed to calculate this for an unknown array.)


    Initially, I thought this might be just a language or compiler anomaly, so I tried Java:

    import java.util.Arrays;
    import java.util.Random;
     
    public class Main
    {
        public static void main(String[] args)
        {
            // Generate data
            int arraySize = 32768;
            int data[] = new int[arraySize];
     
            Random rnd = new Random(0);
            for (int c = 0; c < arraySize; ++c)
                data[c] = rnd.nextInt() % 256;
     
            // !!! With this, the next loop runs faster
            Arrays.sort(data);
     
            // Test
            long start = System.nanoTime();
            long sum = 0;
            for (int i = 0; i < 100000; ++i)
            {
                for (int c = 0; c < arraySize; ++c)
                {   // Primary loop
                    if (data[c] >= 128)
                        sum += data[c];
                }
            }
     
            System.out.println((System.nanoTime() - start) / 1000000000.0);
            System.out.println("sum = " + sum);
        }
    }

    With a similar but less extreme result.

    My first thought was that sorting brings the data into the cache, but then I thought how silly that was because the array was just generated.

    What is going on?

    Why is processing a sorted array faster than processing an unsorted array?

    https://hkrtrainings.com/array-length-in-java

    The code is summing up some independent terms, so the order should not matter.
    Last edited by veerablog; May 5th, 2022 at 11:51 PM.

Similar Threads

  1. unsorted array list (Exception in thread "main" java.lang.NullPointerException)
    By creativeguitar in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 9th, 2013, 06:42 PM
  2. create an array of 99 random values; sorted and the printed
    By appoldk@yahoo.com in forum Java Theory & Questions
    Replies: 1
    Last Post: December 2nd, 2012, 04:57 PM
  3. Unsorted Arrays having trouble controlling the array.
    By orbin in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 16th, 2012, 11:20 AM
  4. Replies: 0
    Last Post: April 15th, 2012, 12:44 AM
  5. How to create a sorted set from the contents of an array
    By davie in forum Collections and Generics
    Replies: 1
    Last Post: March 11th, 2010, 03:44 PM

Tags for this Thread