Forum posts for -system process-

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

Here is a piece of C++ code that seems very peculiar. For some strange reason, sorting the data miraculously makes the code 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)
{
// 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 it in Java:

import java.util.Arrays;
import java.util.Random;

public id 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 somewhat similar, but less extreme result.



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


What is going on?
Why is a sorted array faster than an unsorted array?
The code is summing up some independent terms, and the order should not matter.

View complete forum thread with replies

Other posts related to -system process-

See Related Forum Messages: Follow the Links Below to View Complete Thread

Why is processing a sorted array faster than an unsorted array?
What is the difference between a process and a thread
How to measure actual memory usage of an application or process?
How do you parse and process HTML/XML in PHP?
What is the preferred process for sellling a personal project/product? [closed]
How do I get the application exit code from a Windows command line?
Respecting Fellow Developers [closed]
Why is processing a sorted array faster than an unsorted array?

What is the carbon footprint of your coffee?

Is it low? Is it high? Can this things really kill the planet Earth? Maybe the answer will surprise you. Maybe not.