• Guest, To prevent spam, the BrokenLens Forums are not accepting new registrations at this time.

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

RubbaDubRob

New Member
Joined
Feb 12, 2020
Messages
12
Reaction score
31
Here is a piece of C++ code that shows some very peculiar behavior. For some strange reason, sorting the data miraculously makes the code almost six times faster:

Code:
#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)
   {
       // Primary loop
       for (unsigned c = 0; c < arraySize; ++c)
       {
           if (data[c] >= 128)
               sum += data[c];
       }
   }

   double elapsedTime = static_cast<double>(clock() - start) / CLOCKS_PER_SEC;

   std::cout << elapsedTime << std::endl;
   std::cout << "sum = " << sum << std::endl;
}
  • Without std::sort(data, data + arraySize);, the code runs in 11.54 seconds.
  • With the sorted data, the code runs in 1.93 seconds.
Initially, I thought this might be just a language or compiler anomaly, so I tried Java:

Code:
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)
       {
           // Primary loop
           for (int c = 0; c < arraySize; ++c)
           {
               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?
The code is summing up some independent terms, so the order should not matter.
 
Last edited:
ahem. what
View reply.
 
i don’t speak whatever that language is
View reply.
 
Nice copypasta
View reply.
 

Attachments

  • D017D689-D6DD-4DEB-9A9C-D9AE314DAC7E.jpeg
    D017D689-D6DD-4DEB-9A9C-D9AE314DAC7E.jpeg
    466.6 KB · Views: 14
oh guys u don't understand what he's saying. Jeez online schooling must be going rough for u I guess. I understand everything.

ha. ha.
View reply.
 
Sorted arrays just tend to work faster. Not sure why. Maybe something with how the computer stores data.
View reply.
 

Users Who Are Viewing This Thread (Total: 0, Members: 0, Guests: 0)

Back
Top