Skip to content

Instantly share code, notes, and snippets.

@chenyukang
Created July 11, 2012 12:59
Show Gist options
  • Save chenyukang/3090254 to your computer and use it in GitHub Desktop.
Save chenyukang/3090254 to your computer and use it in GitHub Desktop.
branch-pre
#include <algorithm>
#include <ctime>
#include <iostream>
int main()
{
// generate data
const unsigned arraySize = 32768;
int data[arraySize];
for (unsigned c = 0; c < arraySize; ++c)
data[c] = std::rand() % 256;
std::sort(data, data + arraySize); //排序这行不注释掉下面的for循环会快得多
// test
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)
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