Optimization common set of constants

The following constants are used to optimize instruction set of code.

Constant optimization even if the increase forty-eight line also ran but inefficient algorithm, or algorithms optimization and pruning depends on nature. Not perfect, wait supplement.

But it stuck constant feeling really good sad ah ......

1.sync accelerate cin and cout for input and output of.

Usually when you use absolute felt, iostream stream input and output operators slower than stdio's not the slightest bit, and sometimes even the algorithm is correct, it is because he just used to stream input and output cards to TLE. This phenomenon is due to guarantee the security of iostream has a buffer, data is input to the first variable inlet or refill the buffer to stdout, the instruction can be closed by using this synchronization iostream and stdio, for input and output optimization of about 2 to 3 times.

ios::sync_with_stdio(false);

2. The compiler comes Optmize

This should be no wonder known to everybody, at the beginning of the code with an optimized time directly reduce O3 double and a minority. (Said O2 and O3 have been alternately stacked better, competition disabled)

#pragma GCC optimize(3,"Ofast","inline")//O3优化
#pragma GCC optimize(2)//O2优化

Network transmission line forty acceleration limit, sometimes very fierce, sometimes with no hair.

#pragma GCC optimize(3)
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")

Optimization for the max and min

Damn max and min function, once the two variable data to be compared are not the same type, and even long long int will be given, but also very slow, the STL urinary standard. However, using the ternary operator can not only speed, but also to solve problems between different data types can not be compared.

#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)

Register optimize repeated calls variables

Like a cycle where i and j, in a program will be called many times, from the hardware level, if each layer cycle uses temporary variables newly defined absorption values ​​of the variables from within the reservoir will produce unnecessary consumption, when using the variable before the variable name with this modifier can register where possible to reduce this variable on the register call consumption.

int i,j,n=input;
for(register i=1;i<=n;i++)
    for(register j=1;j<=n;j++)
        do something;

Inline optimize calls to functions

If you call a function in the main function, the implementation is to jump to the start address of the calling function runs to the end of the address after the jump back into place. This call overhead will be very large when multiple calls, especially for those who simply call the function body function of the number. So as define macros, inline code attached directly to the body of the function call overhead is reduced position of the main function. If the function itself is expanded or there is a higher degree of complexity of other operations, such optimization may not be useful or may be ignored by the compiler.

int tmp;
inline int reply(int a,int b){
    register tmp=a;
    a=b;
    b=tmp;
}

Read, read, suddenly found a man of God blog, inside fast read and write speed must be above things all have, have-bit computing to reduce the complexity of the time, and perhaps this is the basic operation of the provincial election OIER level it ......

Link Paste: CNBlog

I'm Schwarzkopf Henkal.

Guess you like

Origin www.cnblogs.com/Schwarzkopf-Henkal/p/11698987.html