Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Copy vs Reference
#include <iostream> #include <chrono> #include <array> #define USE_BRANCH 0 struct TestStruct { // int val0{ 1 }; int val1{ 2 }; int val2{ 3 }; // std::array<int, 100> padding{}; // bool val3{}; }; std::array<TestStruct, 200> values{}; static int sum; static void Copy() { for (const TestStruct ts : values) { #if USE_BRANCH if (ts.val3) #endif { sum += ts.val1; sum += ts.val2; } } } static void Reference() { for (const TestStruct& ts : values) { #if USE_BRANCH if (ts.val3) #endif { sum += ts.val1; sum += ts.val2; } } } int main() { srand(time(0)); std::cout << "Struct: \t" << sizeof(TestStruct) << " bytes\n"; sum = 0; for (TestStruct& ts : values) { ts.val1 = rand() % 100; ts.val2 = rand() % 100; } constexpr size_t NUM_ITERS = 10'000; long long copyTime, refTime; { const auto start = std::chrono::high_resolution_clock::now(); for (size_t i = 0; i < NUM_ITERS; ++i) { Copy(); } const auto end = std::chrono::high_resolution_clock::now(); copyTime = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count(); std::cout << "Copy: \t\t" << copyTime << " microseconds\n"; std::cout << "Sum: \t\t" << sum << "\n"; } sum = 0; for (TestStruct& ts : values) { ts.val1 = rand() % 100; ts.val2 = rand() % 100; } { const auto start = std::chrono::high_resolution_clock::now(); for (size_t i = 0; i < NUM_ITERS; ++i) { Reference(); } const auto end = std::chrono::high_resolution_clock::now(); refTime = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count(); std::cout << "Reference: \t" << refTime << " microseconds\n"; std::cout << "Sum: \t\t" << sum << "\n"; } std::cout << "\nRef is " << 1.0 / (double(refTime) / double(copyTime)) << "x the speed of Copy"; }
run
|
edit
|
history
|
help
0
Hangman
Computing factorial of an integer with recursion and iteration
Competitive - Algorithm for max number of superior characters
boost::asyc fail with error C2280: attempting to reference a deleted function
Template function declaration to avoid usage of template in T::template f<int>()
amusing overload choice
class with unique_ptr to vector
Not Common Numbers
hangman
fusion adapt 64 members v2