#include #include #include using namespace std; int main() { // Generate data const unsigned arraySize = 20000; int data[arraySize]; for (unsigned c = 0; c < arraySize; ++c) data[c] = rand() % 256; // Random values between 0-255 // Sort the arrray. This affects the runnigng-time of the program sort(data, data + arraySize); // Test cout << "Program is running..." << endl; 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) // Branch prediction! sum += data[c]; } } double elapsedTime = static_cast(clock() - start) / CLOCKS_PER_SEC; cout << "Elapsed Time= " << elapsedTime << " seconds" << endl; cout << endl << "sum = " << sum << endl; }