fixed the error part of the question.
Source Link
MuhammadAliDEV
  • 745
  • 3
  • 13
  • 18

Example 2: Think of a telephone book

All the names are sorted. Look for ‘Johnson’ in the book. I bet it will take you less than 3 minutes. As you know where J is in the alphabet now pretend you have those same 60,000 names and phone numbers - unsorted in the same type of book, all jumbled up. no particular order now go find Johnson in the book. I bet it will take you hours! It could be on page 3, or page 349; you never know where it is.

Example 2: Think of a telephone book

All the names are sorted. Look for ‘Johnson’ in the book. I bet it will take you less than 3 minutes. As you know where J is in the alphabet now pretend you have those same 60,000 names and phone numbers - unsorted in the same type of book, all jumbled up. no particular order now go find Johnson in the book. I bet it will take you hours! It could be on page 3, or page 349; you never know where it is.

This isn't Javascript. Also, the compiler has nothing to do with this effect, other than having created branchy asm in the first place instead of using branchless count += (arr[i] < N/2); with x86 setcc/add, or ARM64 cinc. (Or SIMD vectorization.)
Source Link
Peter Cordes
  • 276.6k
  • 41
  • 499
  • 706

The if condition checks that arr[i] < 5000, but if you observe in case of a sorted array, after passing the number 5000 the condition is always false, and before that, it is always true, compiler optimizes the code here. The CPU will recognize that pattern and skips the if conditionbe able to predict correctly which is referred asinstruction to execute next after the conditional branch prediction, instead of sometimes having to rewind after guessing wrong.

allAll the names are sorted. lookLook for ‘johnson’‘Johnson’ in the book. I bet it will take you less than 3 minutes. asAs you know where J is in the alphabet now pretend you have those same 60,000 names and phone numbers - unsorted in the same type of book, all jumbled up. no particular order now go find johnsonJohnson in the book. I bet it will take you hours! as itIt could be on page 3, or page 349.,349; you never know where it is.

The if condition checks that arr[i] < 5000, but if you observe in case of a sorted array, after passing the number 5000 the condition is always false, and before that, it is always true, compiler optimizes the code here and skips the if condition which is referred as branch prediction.

all the names are sorted. look for ‘johnson’ in the book. I bet it will take you less than 3 minutes. as you know where J is in the alphabet now pretend you have those same 60,000 names and phone numbers - unsorted in the same type of book, all jumbled up. no particular order now go find johnson in the book. I bet it will take you hours! as it could be on page 3, or page 349., you never know where it is.

The if condition checks that arr[i] < 5000, but if you observe in case of a sorted array, after passing the number 5000 the condition is always false, and before that, it is always true. The CPU will recognize that pattern and be able to predict correctly which instruction to execute next after the conditional branch, instead of sometimes having to rewind after guessing wrong.

All the names are sorted. Look for ‘Johnson’ in the book. I bet it will take you less than 3 minutes. As you know where J is in the alphabet now pretend you have those same 60,000 names and phone numbers - unsorted in the same type of book, all jumbled up. no particular order now go find Johnson in the book. I bet it will take you hours! It could be on page 3, or page 349; you never know where it is.

This isn't Javascript
Source Link
Peter Cordes
  • 276.6k
  • 41
  • 499
  • 706

// CPP program to demonstrate processing
// time of sorted and unsorted array
#include <iostream>
#include <algorithm>
#include <ctime>
using namespace std;

const int N = 100001;

int main()
{
    int arr[N];

    // Assign random values to array
    for (int i=0; i<N; i++)
        arr[i] = rand()%N;

    // for loop for unsorted array
    int count = 0;
    double start = clock();
    for (int i=0; i<N; i++)
        if (arr[i] < N/2)
            count++;

    double end = clock();
    cout << "Time for unsorted array :: "
        << ((end - start)/CLOCKS_PER_SEC)
        << endl;
    sort(arr, arr+N);

    // for loop for sorted array
    count = 0;
    start = clock();

    for (int i=0; i<N; i++)
        if (arr[i] < N/2)
            count++;

    end = clock();
    cout << "Time for sorted array :: "
        << ((end - start)/CLOCKS_PER_SEC)
        << endl;

    return 0;
}

// CPP program to demonstrate processing
// time of sorted and unsorted array
#include <iostream>
#include <algorithm>
#include <ctime>
using namespace std;

const int N = 100001;

int main()
{
    int arr[N];

    // Assign random values to array
    for (int i=0; i<N; i++)
        arr[i] = rand()%N;

    // for loop for unsorted array
    int count = 0;
    double start = clock();
    for (int i=0; i<N; i++)
        if (arr[i] < N/2)
            count++;

    double end = clock();
    cout << "Time for unsorted array :: "
        << ((end - start)/CLOCKS_PER_SEC)
        << endl;
    sort(arr, arr+N);

    // for loop for sorted array
    count = 0;
    start = clock();

    for (int i=0; i<N; i++)
        if (arr[i] < N/2)
            count++;

    end = clock();
    cout << "Time for sorted array :: "
        << ((end - start)/CLOCKS_PER_SEC)
        << endl;

    return 0;
}

timing of execautiontiming of execution

// CPP program to demonstrate processing
// time of sorted and unsorted array
#include <iostream>
#include <algorithm>
#include <ctime>
using namespace std;

const int N = 100001;

int main()
{
    int arr[N];

    // Assign random values to array
    for (int i=0; i<N; i++)
        arr[i] = rand()%N;

    // for loop for unsorted array
    int count = 0;
    double start = clock();
    for (int i=0; i<N; i++)
        if (arr[i] < N/2)
            count++;

    double end = clock();
    cout << "Time for unsorted array :: "
        << ((end - start)/CLOCKS_PER_SEC)
        << endl;
    sort(arr, arr+N);

    // for loop for sorted array
    count = 0;
    start = clock();

    for (int i=0; i<N; i++)
        if (arr[i] < N/2)
            count++;

    end = clock();
    cout << "Time for sorted array :: "
        << ((end - start)/CLOCKS_PER_SEC)
        << endl;

    return 0;
}

timing of execaution

// CPP program to demonstrate processing
// time of sorted and unsorted array
#include <iostream>
#include <algorithm>
#include <ctime>
using namespace std;

const int N = 100001;

int main()
{
    int arr[N];

    // Assign random values to array
    for (int i=0; i<N; i++)
        arr[i] = rand()%N;

    // for loop for unsorted array
    int count = 0;
    double start = clock();
    for (int i=0; i<N; i++)
        if (arr[i] < N/2)
            count++;

    double end = clock();
    cout << "Time for unsorted array :: "
        << ((end - start)/CLOCKS_PER_SEC)
        << endl;
    sort(arr, arr+N);

    // for loop for sorted array
    count = 0;
    start = clock();

    for (int i=0; i<N; i++)
        if (arr[i] < N/2)
            count++;

    end = clock();
    cout << "Time for sorted array :: "
        << ((end - start)/CLOCKS_PER_SEC)
        << endl;

    return 0;
}

timing of execution

Stack Overflow is like an encyclopedia, so we prefer to omit these types of phrases. It is assumed that everyone here is trying to be helpful.
Source Link
Dharman
  • 26.3k
  • 20
  • 67
  • 119
Loading
Source Link
MuhammadAliDEV
  • 745
  • 3
  • 13
  • 18
Loading