Topic Text   Topic Comments (0)   Topic Properties   Topic Information cody@at
Topic title: a Saturday August 4, 2012 19:36:26

Download topic text | View in monospace font | Tab width set to 8 (change to 4)

Files in topic: (view all files)  
hello.cc   {+37,-0}

[Add General Comment] to topic.

File hello.cc (Revision 1.0) [Add File Comment] [Top]
 
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 }
 
File hello.cc (Revision 1.0) [Add File Comment] [Top]
  
Legend:
Removed 
Changed
 Added

[Add General Comment] to topic.