#include <iostream>
#include <chrono>
#include <algorithm>
using namespace std;
using namespace std::chrono;
constexpr size_t size = 10000000;
void fill_array (int T[size]) {
for (size_t counter = 0, i = 0; counter < size / 2; ++i) {
if (i == size - 1)
i = 0;
if (rand() % 3 == 0 && T[i] == 0) {
T[i] = rand();
++counter;
}
}
}
size_t solution_a (const int T[size]) {
size_t sum = 0;
for (size_t i = 0; i < size; ++i)
sum += T[i];
return sum;
}
size_t solution_b (const int T[size]) {
size_t sum = 0;
for (size_t i = 0; i < size; ++i) {
if (T[i] != 0) {
sum += T[i];
}
}
return sum;
}
int main () {
int *Array = new int[size];
fill_array(Array);
//sort(Array, Array + size);
{
high_resolution_clock::time_point start = high_resolution_clock::now();
cout << solution_a(Array) << endl;
high_resolution_clock::time_point stop = high_resolution_clock::now();
auto time_span = duration_cast<chrono::microseconds>(stop - start);
std::cout << "solution_a took " << time_span.count() << " microseconds" << endl;
}
// ----------------------------------------------------------------------
{
high_resolution_clock::time_point start = high_resolution_clock::now();
cout << solution_b(Array) << endl;
high_resolution_clock::time_point stop = high_resolution_clock::now();
auto time_span = duration_cast<chrono::microseconds>(stop - start);
std::cout << "solution_b took " << time_span.count() << " microseconds" << endl;
}
delete[] Array;
return 0;
}