| |
| 1 |
#include <algorithm> |
| 2 |
#include <ctime> |
| 3 |
#include <iostream> |
| 4 |
|
| 5 |
int main() |
| 6 |
{ |
| 7 |
// generate data |
| 8 |
const unsigned arraySize = 32768; |
| 9 |
int data[arraySize]; |
| 10 |
|
| 11 |
for (unsigned c = 0; c < arraySize; ++c) |
| 12 |
data[c] = std::rand() % 256; |
| 13 |
|
| 14 |
|
| 15 |
// !!! with this, the next loop runs faster |
| 16 |
std::sort(data, data + arraySize); |
| 17 |
|
| 18 |
|
| 19 |
// test |
| 20 |
clock_t start = clock(); |
| 21 |
long long sum = 0; |
| 22 |
|
| 23 |
for (unsigned i = 0; i < 100000; ++i) |
| 24 |
{ |
| 25 |
// primary loop |
| 26 |
for (unsigned c = 0; c < arraySize; ++c) |
| 27 |
{ |
| 28 |
if (data[c] >= 128) |
| 29 |
sum += data[c]; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
double elapsedTime = static_cast<double>(clock() - start) / CLOCKS_PER_SEC; |
| 34 |
|
| 35 |
std::cout << elapsedTime << std::endl; |
| 36 |
std::cout << "sum = " << sum << std::endl; |
| 37 |
} |
| |