DEV Community

Qzhang125
Qzhang125

Posted on • Updated on

SPO 600 Week 7 Reflection

Hello my friend, welcome to the week 7 of SPO600(Software Portability and Optimization) blog. In this blog we will discuss about the compiler optimization.

Introduction

Compiler optimizations are alternations that are made by a compiler to keep the same result as the original code but achieve the best performance. It usually means reducing execution time, reducing code size, and improving execution speed. In other words, compiler optimization is going to make the code as efficient as it could.

Performance##

Let's look at an example.

void foo(int size) {
    for (int i = 0; i < 5; i++) {
        if (size == 10) {
            std::cout << "Size is 10" << std::endl;
        }
        else {
            std::cout << "Keep going" << std::endl;
        }
    }
}

void betterFoo(int size) {
    if (size == 10) {
        for (int i = 0; i < 5; i++) {
            std::cout << "Size is 10" << std::endl;
        }
    }
    else {
        for (int i = 0; i < 5; i++) {
            std::cout << "Keep going" << std::endl;
        }
    }
}



int main() {
    cout << "Foo" << endl;
    foo(10);
    cout << "better foo" << endl;
    betterFoo(10);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

The result:
Image description
In this c++ program, I created 2 functions. The first function foo is going to print 5 times if the “size is 10” or “keep going” message. The second function betterFoo is doing the same thing. The difference between them is the “size” value is never changed and it is executed 5 times in the loop. In the betterFoo function, the size is only checked once before doing the loop.

Reflection

In this week, we worked on many different compiler optimizations, each of them looks tiny but they are able to improve our program to be faster and time efficient because the algorithm is super important for a program. It controls the sequence of execution between each method and functions, every tiny improvement will make the final solution to be a bit better.

Discussion (0)