C++ 细致入微的优化

1.register

暗示某变量放在寄存器中 提取较快

2.++ -- 的表达

++a 快于 a++ 因为后者要记录 a 这个变量到++完以后 而前者搜到变量后直接+了 --相同

主要 某些地方还是不能换的 例如 while (a--) 之类

3.多用但不滥用位运算

如 /2 可以改成 >> 1 然后 a /= 2 可以改成 a >>= 1 还有个>=是什么我不知道=-=

然后读入输出优化 我喜欢把 48 表示为 ((1 << 1 | 1) << (1 << (1 << 1))) 虽然感觉很神经=-=

4.灵活运用但不滥用define

也不要乱用啊 =-= define是个好东西 举个例子

#define for(i,a,b) for(int i = a ; i <= b ; ++ i)
blabblab()
{
	scanf("%d",&n);
	for(i,1,n) scanf("%d",&v[i]);
}


//又如某优化的 ×10
#define ten(a) ((a << (1 << 1 | 1)) + (a << 1)) //就是八倍a加二倍a
blabblab(int &x)
{
	char q = getchar();
	while ('0' <= q && q <= '9') x = ten(x) + q - 48,q = getchar();
}

猜你喜欢

转载自blog.csdn.net/Frocean/article/details/81129084