Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is it faster to process a sorted array than an unsorted array?
    primarykey
    data
    text
    <p>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.</p> <pre class="lang-cpp prettyprint-override"><code>#include &lt;algorithm&gt; #include &lt;ctime&gt; #include &lt;iostream&gt; int main() { // Generate data const unsigned arraySize = 32768; int data[arraySize]; for (unsigned c = 0; c &lt; 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 &lt; 100000; ++i) { // Primary loop for (unsigned c = 0; c &lt; arraySize; ++c) { if (data[c] &gt;= 128) sum += data[c]; } } double elapsedTime = static_cast&lt;double&gt;(clock() - start) / CLOCKS_PER_SEC; std::cout &lt;&lt; elapsedTime &lt;&lt; std::endl; std::cout &lt;&lt; "sum = " &lt;&lt; sum &lt;&lt; std::endl; } </code></pre> <ul> <li>Without <code>std::sort(data, data + arraySize);</code>, the code runs in 11.54 seconds.</li> <li>With the sorted data, the code runs in 1.93 seconds.</li> </ul> <p>Initially, I thought this might be just a language or compiler anomaly. So I tried it in Java.</p> <pre class="lang-java prettyprint-override"><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 &lt; 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 &lt; 100000; ++i) { // Primary loop for (int c = 0; c &lt; arraySize; ++c) { if (data[c] &gt;= 128) sum += data[c]; } } System.out.println((System.nanoTime() - start) / 1000000000.0); System.out.println("sum = " + sum); } } </code></pre> <p>With a somewhat similar but less extreme result.</p> <hr> <p>My first thought was that sorting brings the data into the cache, but then I thought how silly that is because the array was just generated.</p> <ul> <li>What is going on?</li> <li>Why is it faster to process a sorted array than an unsorted array?</li> <li>The code is summing up some independent terms, and the order should not matter.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload