S
speed258
Guest
Hello Can anyone say why this code took to execute ~30minutes, I have played with compiler settings and code was executed in 2 minutes, I cant remember what I just did
Code below:
// sampleC.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <algorithm>
#include <vector>
#include <chrono>
#include <cmath>
#include <numeric> // iota
using namespace std;
using namespace chrono;
using Clock = chrono::steady_clock;
using Int = uint64_t;
struct Node {
Int payload; // ignored; just for plausability.
Node* next = nullptr;
};
static_assert(sizeof(Node) == 16, "Not 64-bit? That's OK too.");
double time(Int N, Int iters)
{
vector<Node> memory(N);
vector<Node*> nodes(N);
for (Int i = 0; i < N; ++i) {
nodes = &memory;
}
std::random_shuffle(begin(nodes), end(nodes));
// Link up the nodes:
for (Int i = 0; i < N - 1; ++i) {
nodes->next = nodes[i + 1];
}
Node* start_node = nodes[0];
nodes.clear();
nodes.shrink_to_fit(); // Free up unused memory before meassuring:
// Do the actual measurements:
auto start = Clock::now();
for (Int it = 0; it < iters; ++it) {
// Run through all the nodes:
Node* node = start_node;
while (node)
{
node = node->next;
}
}
auto dur = Clock::now() - start;
auto ns = duration_cast<nanoseconds>(dur).count();
return ns / double(N * iters);
}
int main()
{
double avg = 0;
cout << "#bytes ns/elem" << endl;
try
{
Int stopsPerFactor = 4; // For every power of 2, how many measurements do we do?
Int minElemensFactor = 6; // First measurement is 2^this number of elements.
Int maxElemsFactor = 30; // Last measurement is 2^this number of elements. 30 == 16GB of memory
Int min = stopsPerFactor * minElemensFactor;
Int max = stopsPerFactor * maxElemsFactor;
for (Int ei = min; ei <= max; ++ei)
{
Int N = (Int)round(pow(2.0, double(ei) / stopsPerFactor));
//Int reps = elemsPerMeasure / N;
Int reps = (Int)round(2e10 / pow(N, 1.5));
if (reps < 1) reps = 1;
auto ans = time(N, reps);
avg = avg + ans;
cout << (N * sizeof(Node)) << " " << ans << " # (N=" << N << ", reps=" << reps << ") " << (ei - min + 1) << "/" << (max - min + 1) << endl;
}
cout << avg / max << endl;
}
catch (exception& e)
{
cout << "# stopped due to exception: " << e.what() << endl;
}
return 0;
}
Compiler settings:
What did I do wrong in compiler settings?
Continue reading...
Code below:
// sampleC.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <algorithm>
#include <vector>
#include <chrono>
#include <cmath>
#include <numeric> // iota
using namespace std;
using namespace chrono;
using Clock = chrono::steady_clock;
using Int = uint64_t;
struct Node {
Int payload; // ignored; just for plausability.
Node* next = nullptr;
};
static_assert(sizeof(Node) == 16, "Not 64-bit? That's OK too.");
double time(Int N, Int iters)
{
vector<Node> memory(N);
vector<Node*> nodes(N);
for (Int i = 0; i < N; ++i) {
nodes = &memory;
}
std::random_shuffle(begin(nodes), end(nodes));
// Link up the nodes:
for (Int i = 0; i < N - 1; ++i) {
nodes->next = nodes[i + 1];
}
Node* start_node = nodes[0];
nodes.clear();
nodes.shrink_to_fit(); // Free up unused memory before meassuring:
// Do the actual measurements:
auto start = Clock::now();
for (Int it = 0; it < iters; ++it) {
// Run through all the nodes:
Node* node = start_node;
while (node)
{
node = node->next;
}
}
auto dur = Clock::now() - start;
auto ns = duration_cast<nanoseconds>(dur).count();
return ns / double(N * iters);
}
int main()
{
double avg = 0;
cout << "#bytes ns/elem" << endl;
try
{
Int stopsPerFactor = 4; // For every power of 2, how many measurements do we do?
Int minElemensFactor = 6; // First measurement is 2^this number of elements.
Int maxElemsFactor = 30; // Last measurement is 2^this number of elements. 30 == 16GB of memory
Int min = stopsPerFactor * minElemensFactor;
Int max = stopsPerFactor * maxElemsFactor;
for (Int ei = min; ei <= max; ++ei)
{
Int N = (Int)round(pow(2.0, double(ei) / stopsPerFactor));
//Int reps = elemsPerMeasure / N;
Int reps = (Int)round(2e10 / pow(N, 1.5));
if (reps < 1) reps = 1;
auto ans = time(N, reps);
avg = avg + ans;
cout << (N * sizeof(Node)) << " " << ans << " # (N=" << N << ", reps=" << reps << ") " << (ei - min + 1) << "/" << (max - min + 1) << endl;
}
cout << avg / max << endl;
}
catch (exception& e)
{
cout << "# stopped due to exception: " << e.what() << endl;
}
return 0;
}
Compiler settings:
What did I do wrong in compiler settings?
Continue reading...