程序优化技巧

发布时间:2025-01-04 21:57

定期更新耳机驱动程序优化 #生活技巧# #数码产品使用技巧# #耳机音质优化指南#

C++卡常

最新推荐文章于 2024-07-07 19:34:57 发布

置顶 zsyz_ZZY 于 2018-04-16 15:43:53 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

1.读入优化(*)

低阶版:

inline int read()

{

int x=0,f=1;

char ch=getchar();

for(;ch<'0'||ch>'9';ch=getchar())

if(ch=='-') f=-1;

for(;ch>='0'&&ch<='9';x=(x<<3)+(x<<1)+(ch^48),ch=getchar());

return x*f;

}

高阶版:

inline char getc()

{

static char buf[1<<20],*fs,*ft;

return(fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin)),fs==ft)?EOF:*fs++;

}

inline void read(int &x)

{

char ch=getc();

for(x=0;ch=='\n'||ch==' ';ch=getc());

for(;ch!='\n'&&ch!=' '&&ch!=EOF;ch=getc())

x=((x+(x<<2))<<1)+(ch^0x30);

}

inline void read_str(char *str)

{

char ch=getc();

for(;ch<'0'||ch>'9';ch=getc());

for(;ch<'0'||ch>'9';ch=getc())

*str=ch,str++;

*str='\0';

}

2.输出优化

低阶版:

void print(int x)

{

if(x<0)

{

putchar('-');

x=-x;

}

if(x>9) print(x/10);

putchar(x%10+'0');

}

高阶版:

static const int BUF=50000000;

char buf[BUF],*h=buf;

inline void put(char ch)

{

h==buf+BUF?(fwrite(buf,1,BUF,stdout),h=buf):0;

*h++=ch;

}

inline void putint(int num)

{

static char _buf[30];

sprintf(_buf,"%d",num);

for(char *s=_buf;*s;s++)put(*s);

}

inline void finish()

{

fwrite(buf,1,h-buf,stdout);

}

注意:put输出字符类型的,putint输出整数类型的。并且在主程序结束前打上finish();

3.register

举个例子for(int i=1;i<=n;i++)   =>   for(register int i=1;i<=n;i++)

4.inline

举个例子int add(int x,int y){return x+y;}   =>   inline int add(int x,int y){return x+y;}

5.位运算(*)

举个例子x*=2   =>   x<<=1

6.减少使用STL(*)

STL是一个常数非常大的东西。

7.用上define

define比赋值要快!!!

只要你的“暴力”不是很“暴力”,TLE1~2个点时,应该够用了。

带*号的真的很有用!!!

最后,感谢LJY大佬对我卡常知识的指导!!!

网址:程序优化技巧 https://www.yuejiaxmz.com/news/view/644488

相关内容

浅谈简单的程序优化技巧(C++)
Python 程序的运行速度优化技巧
新旧手机都适用的优化技巧:程序员教你如何提升手机性能
常数优化技巧
小程序性能优化指南
Linux启动时间优化技巧
程序员的工作生活平衡技巧 – PingCode
[C/C++代码优化实战指南]:如何提高程序性能?
流程优化技巧.ppt
动态规划优化技巧.doc

随便看看