Files
CSCI-1200/lectures/optimization/gprof/test.cpp
2025-02-26 12:16:53 -05:00

26 lines
683 B
C++

#include <iostream>
void heavyComputation() {
// The use of volatile prevents compiler optimizations that could remove the loops.
volatile long long sum = 0;
for (long long i = 0; i < 500000000; ++i) {
sum += i; // Simple but expensive loop
}
}
void lightComputation() {
// The use of volatile prevents compiler optimizations that could remove the loops.
volatile int sum = 0;
for (int i = 0; i < 100000; ++i) {
sum += i; // Lighter loop
}
}
int main() {
heavyComputation(); // Call heavy function once
for (int i = 0; i < 1000; ++i) {
lightComputation(); // Call light function many times
}
return 0;
}