adding test.cpp

This commit is contained in:
Jidong Xiao
2025-02-24 22:19:25 -05:00
committed by JamesFlare
parent 33c771a3a5
commit e275aa3ad7
2 changed files with 26 additions and 1 deletions

View File

@@ -15,7 +15,7 @@ $ g++ -pg -o test test.cpp
``` ```
## Example C++ Program ## Example C++ Program
Create a file `test.cpp` with the following code: Create a file [test.cpp](test.cpp) with the following code:
```cpp ```cpp
#include <iostream> #include <iostream>

View File

@@ -0,0 +1,25 @@
#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;
}