Skip to content

Instantly share code, notes, and snippets.

@tritao
Created February 22, 2025 01:15
Show Gist options
  • Save tritao/aaf54e35b86bc7f3f66a8756e4b4bbda to your computer and use it in GitHub Desktop.
Save tritao/aaf54e35b86bc7f3f66a8756e4b4bbda to your computer and use it in GitHub Desktop.
#include <iostream>
#include <chrono>
struct Color {
float a, b, c, d;
};
volatile float sink = 0.0f;
#if defined(_MSC_VER)
#define NOINLINE __declspec(noinline)
#else
#define NOINLINE __attribute__((noinline))
#endif
void pass_by_value(Color col) {
sink = col.a + col.b + col.c + col.d;
}
NOINLINE void pass_by_const_ref(const Color &col) {
sink = col.a + col.b + col.c + col.d;
}
int main() {
const int iterations = 100000000;
Color col { 1.0f, 2.0f, 3.0f, 4.0f };
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < iterations; ++i) {
pass_by_value(col);
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed_value = end - start;
std::cout << "Time pass_by_value: " << elapsed_value.count() << " s\n";
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < iterations; ++i) {
pass_by_const_ref(col);
}
end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed_const_ref = end - start;
std::cout << "Time pass_by_const_ref: " << elapsed_const_ref.count() << " s\n";
return 0;
}
@tritao
Copy link
Author

tritao commented Feb 22, 2025

g++ -O2 -std=c++11 profile.cpp -o profile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment