Created
August 2, 2022 09:24
-
-
Save Zheaoli/a3970ed91a9f9137d6d1c8f2def58292 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <ctime> | |
#include <iostream> | |
int main() | |
{ | |
const unsigned arraySize = 32768; | |
int data[arraySize]; | |
for (unsigned c = 0; c < arraySize; ++c) | |
data[c] = std::rand() % 256; | |
std::sort(data, data + arraySize); | |
clock_t start = clock(); | |
long long sum = 0; | |
for (unsigned i = 0; i < 100000; ++i) | |
{ | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment