Skip to content

Instantly share code, notes, and snippets.

@0dm
Last active May 5, 2023 02:00
Show Gist options
  • Save 0dm/12250a2f0e56216a54db72def97249d0 to your computer and use it in GitHub Desktop.
Save 0dm/12250a2f0e56216a54db72def97249d0 to your computer and use it in GitHub Desktop.
GCC Optimization Example
#include <chrono>
#include <iostream>
int test() {
long long number = 0;
for (long long i = 0; i != 2000000000; ++i) {
number += 3;
}
std::cout << number << "\n";
return 3;
}
template <typename T>
float getExecutionTime(T f) {
auto getTime1 = std::chrono::high_resolution_clock::now();
f();
auto getTime2 = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> fpms = getTime2 - getTime1;
return fpms.count();
}
int main() {
std::cout << getExecutionTime(test) << "ms";
}
g++ example.cpp -o l0.exe
g++ example.cpp -o l1.exe -O
g++ example.cpp -o l2.exe -O2
g++ example.cpp -o l3.exe -O3
l0
l1
l2
l3
pause
all: l0 l1 l2 l3
clean:
rm -f l0 l1 l2 l3
run:
./l0
./l1
./l2
./l3
l0: example.cpp
g++ -o l0 example.cpp -w
l1: example.cpp
g++ -o l1 example.cpp -O -w
l2: example.cpp
g++ -o l2 example.cpp -O2 -w
l3: example.cpp
g++ -o l3 example.cpp -O3 -w
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment