auto&prefetch

本文探讨了C++中使用auto关键字简化代码的方式及其注意事项,并介绍了如何利用prefetch指令预加载内存以减少缓存缺失,提升程序性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

auto项目开始对auto解禁了之后,才开始使用,起码对于vector<xxx>::iterator这样的代码起了很大的简洁的作用。

后来发现在写多个版本的代码的时候也让一切变得很简洁,比如原来是:

#if BONESET
BoneSet* bone = GetBoneRoot();
#else
BoneNode* bone = GetBoneRoot();
#endif

使用auto的时候就可以是

auto bone = GetBoneRoot();

但是其中也容易犯错误,尤其是程序员自大的以为可以不看文档就直接使用(比如当时的我),如果返回的是const type&的话使用auto的时候,需要也带上引用符号和const这样的修饰符,auto演绎出来的只是类型。

msdn上面的文档:

The auto keyword deduces the type of a declared variable from its initialization expression.

To use the auto keyword, declare a variable with the auto keyword instead of a type, and specify an initialization expression. In addition, you can modify the auto keyword with specifiers and declarators such as const, volatile, pointer (*), reference (&), and rvalue reference (&&). The compiler evaluates the initialization expression and then uses that information to deduce the type of the variable.

 

prefetch

就是提前把一部分内存加载到cache里面,防止在用的时候产生cache miss,消耗大量的性能,以前在console上开发的时候常常使用这个,对优化很有好处,在pc上面以为是没有的,但是后来发现sse库里面有这个支持,非常棒:

http://msdn.microsoft.com/en-us/library/cyxt4d09(v=vs.71).aspx


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值