Gene Algorithm Optimization - Using the -O3 Flag

Work on a problem requiring GA. I have everything that works, and spent a lot of time trimming fat and optimizing the code before resorting to compiler optimization. Since GA works as a result of user input, it must find a solution within a reasonable period of time, otherwise the user interface will be closed and it just won't play well. I got this binary GA resolving 27 variable problems in about 0.1 s on iPhone 3GS.

To achieve this level of performance, the entire GA was encoded in C, not Objective-C.

In search of further reduction in runtime, I considered the idea of ​​using the β€œ-O3” optimization switch only for the solver module. I tried, and it reduced the work time by almost half.

Do I have to worry about any reason setting optimization to "-O3"? Keep in mind that I am doing this at the file level, and not for the entire project.

+6
source share
1 answer
Flag

-O3 will make the code work the same way as before (only faster) if you do not do any complicated things that are unsafe or to some extent depend on what the compiler does.

In addition, as suggested in the comments, it would be nice to let the calculation run in a separate thread to prevent the user interface from being blocked. It also gives you the ability to make calculations more expensive or display a progress bar or something else.


Tricky stuff

Optimization will lead to unexpected results if you try to directly access materials on the stack or move the stack pointer to another location or if you make something inherently illegal, for example, forget to initialize the variable (some compilers (MinGW) will set them to 0 )

eg.

 int main() { int array[10]; array[-2] = 13; // some negative value might get the return address } 

Some other tricky things include an optimizer that populates on its own. Here's an example where -O3 completely breaks the code.

+5
source

Source: https://habr.com/ru/post/885590/


All Articles