Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >C++核心准则Per.19:以可以预测的方式访问内存

C++核心准则Per.19:以可以预测的方式访问内存

作者头像
面向对象思考
发布于 2020-06-28 08:54:57
发布于 2020-06-28 08:54:57
45500
代码可运行
举报
运行总次数:0
代码可运行

Per.19: Access memory predictably

Per.19:以可以预测的方式访问内存

Reason(原因)

Performance is very sensitive to cache performance and cache algorithms favor simple (usually linear) access to adjacent data.

程序的性能和缓冲的性能直接相关,而缓冲算法更善于处理简单的(通常是线性的)访问连续数据。

Example(示例)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
int matrix[rows][cols];

// bad
for (int c = 0; c < cols; ++c)
    for (int r = 0; r < rows; ++r)
        sum += matrix[r][c];

// good
for (int r = 0; r < rows; ++r)
    for (int c = 0; c < cols; ++c)
        sum += matrix[r][c];

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#per19-access-memory-predictably

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-06-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
C++核心准则CP.8:不要使用volatile关键字实现同步处理​
CP.8: Don't try to use volatile for synchronization
面向对象思考
2020/07/03
3910
C++核心准则​NL.19:避免容易被误读的名称
Readability. Not everyone has screens and printers that make it easy to distinguish all characters. We easily confuse similarly spelled and slightly misspelled words.
面向对象思考
2020/12/15
5010
C++核心准则​SL.con.1:标准库array或vector好于C数组
C arrays are less safe, and have no advantages over array and vector. For a fixed-length array, use std::array, which does not degenerate to a pointer when passed to a function and does know its size. Also, like a built-in array, a stack-allocated std::array keeps its elements on the stack. For a variable-length array, use std::vector, which additionally can change its size and handles memory allocation.
面向对象思考
2020/10/30
6270
C++核心准则​SL.con.1:标准库array或vector好于C数组
C++核心准则​​SL.con.2:除非有理由使用其他容器,默认使用STL vector
vector and array are the only standard containers that offer the following advantages:
面向对象思考
2020/10/30
4290
C++核心准则​​SL.con.2:除非有理由使用其他容器,默认使用STL vector
C++核心准则​NL.4:保持一致的缩进样式
NL.4: Maintain a consistent indentation style
面向对象思考
2020/11/26
2880
C++核心准则Per.7:设计要为优化做准备
Because we often need to optimize the initial design. Because a design that ignores the possibility of later improvement is hard to change.
面向对象思考
2020/06/24
4640
C++核心准则Per.5,6 关于性能的误解
Low-level code sometimes inhibits optimizations. Optimizers sometimes do marvels with high-level code.
面向对象思考
2020/06/24
3530
C++核心准则CP.200:使用volatile只能表明该变量是非C++内存
volatile is used to refer to objects that are shared with "non-C++" code or hardware that does not follow the C++ memory model.
面向对象思考
2020/07/28
3120
C++核心准则​NL.16:使用常规的类成员声明顺序
A conventional order of members improves readability.
面向对象思考
2020/12/15
8020
C++核心准则​​SL.str.1:使用std::string管理字符序列
string correctly handles allocation, ownership, copying, gradual expansion, and offers a variety of useful operations.
面向对象思考
2020/10/30
5390
C++核心准则​​SL.str.1:使用std::string管理字符序列
C++核心准则C.9:最小限度暴露成员
Encapsulation. Information hiding. Minimize the chance of unintended access. This simplifies maintenance.
面向对象思考
2020/03/25
3940
C++核心准则ES.103​:防止溢出​
Overflow usually makes your numeric algorithm meaningless. Incrementing a value beyond a maximum value can lead to memory corruption and undefined behavior.
面向对象思考
2020/06/17
3970
C++核心准则ES.1: 标准库好于其他库和手写代码
ES.1: Prefer the standard library to other libraries and to "handcrafted code"
面向对象思考
2020/04/16
4240
C++核心准则C.139:谨慎使用final
Capping a hierarchy with final is rarely needed for logical reasons and can be damaging to the extensibility of a hierarchy.
面向对象思考
2020/03/25
4720
C++核心准则E.6:使用RAII防止资源泄露
Leaks are typically unacceptable. Manual resource release is error-prone. RAII ("Resource Acquisition Is Initialization") is the simplest, most systematic way of preventing leaks.
面向对象思考
2020/08/04
3510
C++核心准则C.84:swap函数不应该失败
swap is widely used in ways that are assumed never to fail and programs cannot easily be written to work correctly in the presence of a failing swap. The standard-library containers and algorithms will not work correctly if a swap of an element type fails.
面向对象思考
2020/03/25
4730
C++核心准则C.181:避免使用"暴露的"联合体
A naked union is a union without an associated indicator which member (if any) it holds, so that the programmer has to keep track. Naked unions are a source of type errors.
面向对象思考
2020/03/25
4750
C++核心准则ES.42: 使用指针时要简单且直接
Complicated pointer manipulation is a major source of errors.
面向对象思考
2020/05/20
4690
C++核心准则C.46:默认状态下明确定义单参数构造函数
C.46: By default, declare single-argument constructors explicit
面向对象思考
2020/03/25
6440
C++核心准则Per.1,2,3,4 慎重地优化代码
If there is no need for optimization, the main result of the effort will be more errors and higher maintenance costs.
面向对象思考
2020/06/24
3110
推荐阅读
相关推荐
C++核心准则CP.8:不要使用volatile关键字实现同步处理​
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验