1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <iostream>
#include <ctime>
using namespace std;
// denver2020 code *****************
struct B {
int i[32];
};
int Sample1(B* array, int Sizearray) {
int sum = 0;
for (int a = 0; a<Sizearray; ++a) {
for(int b =0; b<32; ++b) {
sum += array[a].i[b];
}
}
return sum;
}
int Sample2(B* array, int Sizearray) {
int sum = 0;
for (int b=0; b<32; ++b) {
for (int a =0; a<Sizearray; ++a) {
sum += array[a].i[b];
}
}
return sum;
}
// end of denver2020 code *****************
void timeIt( int fn( B*, int ), B* A, int n, int &sum, double &t )
{
clock_t tstart = clock();
sum = fn( A, n );
clock_t tfinish = clock();
t = double( tfinish - tstart ) / CLOCKS_PER_SEC;
}
int main()
{
const int SIZE = 100000;
B *A = new B[SIZE];
for ( int r = 0; r < SIZE; r++ )
{
for ( int c = 0; c < 32; c++ ) A[r].i[c] = c;
}
int sum1, sum2;
double time1, time2;
timeIt( Sample1, A, SIZE, sum1, time1 );
timeIt( Sample2, A, SIZE, sum2, time2 );
cout << "Sample1: "<< sum1 << " in time " << time1 << " s\n";
cout << "Sample2: "<< sum2 << " in time " << time2 << " s\n";
delete [] A;
}
|