当前位置:网站首页>【C语言指针】用指针提升数组的运算效率
【C语言指针】用指针提升数组的运算效率
2022-08-05 10:28:00 【SN-Grotesque】
原本代码
static void test()
{
size_t size = 500 * pow(1024, 2);
uint8_t *data = (uint8_t *)malloc(size);
size_t x;
clock_t start, end;
start = clock();
for(x = 0; x < size; ++x) {
data[x] = 0x41;
}
end = clock();
printf("%lf seconds\n",(double)(end - start) / CLOCKS_PER_SEC);
}
经过十次的执行,取平均值为1.9628
秒
修改后代码
static void test()
{
size_t size = 500 * pow(1024, 2);
uint8_t *data = (uint8_t *)malloc(size);
size_t x;
clock_t start, end;
start = clock();
for(x = 0; x < size; ++x) {
*data = 0x41;
data++;
}
end = clock();
printf("%f seconds\n",(double)(end - start) / CLOCKS_PER_SEC);
}
经过十次执行,取平均值为1.8914
秒
可以看到平均下来指针比使用下标的方式要快上近
0.1
秒,你可能会说就100毫秒有啥好优化的。
那是因为你接触到的项目不够大,如果在一个大型项目上面还是只使用数组下标的话,就比较容易导致整个项目的节奏被某些函数或运算给拖后腿。
最后如果你是一个不够了解指针或C语言的人可能会注意到一个现象。
那就是后续使用printf函数无法输出上面这个data
变量,输出结果是什么也没有。
那是因为指针已经指向了这个变量的最后一个值的下一个值。
简单来说就是p = data[(n - 1) + 1];
p是指针指向的值,n是变量长度。
要解决这个问题只需要使用另一个指针来在一开始指向这个data变量,然后后续的运算都用此新指针。
边栏推荐
- MySQL transactions
- NowCoderTOP35-40 - continuous update ing
- [Android] How to use RecycleView in Kotlin project
- JS introduction to reverse the recycling business network of learning, simple encryption mobile phone number
- FPGA:基础入门LED灯闪烁
- 还在找网盘资源吗?快点收藏如下几个值得收藏的网盘资源搜索神器吧!
- poj2287 Tian Ji -- The Horse Racing(2016xynu暑期集训检测 -----C题)
- RT - Thread record (a, RT, RT Thread version - Thread Studio development environment and cooperate CubeMX quick-and-dirty)
- Technical dry goods | Hausdorff distance for image segmentation based on MindSpore
- The host computer develops C# language: simulates the STC serial port assistant to receive the data sent by the microcontroller
猜你喜欢
电竞、便捷、高效、安全,盘点OriginOS功能的关键词
数据可视化(二)
Still looking for a network backup resources?Hurry up to collect the following network backup resource search artifact it is worth collecting!
The founder of the DFINITY Foundation talks about the ups and downs of the bear market, and where should DeFi projects go?
RT-Thread记录(一、RT-Thread 版本、RT-Thread Studio开发环境 及 配合CubeMX开发快速上手)
Getting started with Polkadot parachain development, this article is enough
首次去中心化抢劫?近2亿美元损失:跨链桥Nomad 被攻击事件分析
Leetcode刷题——623. 在二叉树中增加一行
什么是 DevOps?看这一篇就够了!
[Strong Net Cup 2022] WP-UM
随机推荐
NowCoderTOP35-40——持续更新ing
poj2287 Tian Ji -- The Horse Racing(2016xynu暑期集训检测 -----C题)
如何测试一下现场的备机失败,转发主机的场景?
SkiaSharp 之 WPF 自绘 投篮小游戏(案例版)
linux下oracle常见操作以及日常积累知识点(函数、定时任务)
three物体围绕一周呈球形排列
2022华数杯数学建模思路分析交流
华为轻量级神经网络架构GhostNet再升级,GPU上大显身手的G-GhostNet(IJCV22)
第七章,activiti个人任务分配,动态指定和监听器指定任务委派人「建议收藏」
FPGA: Basic Getting Started LED Lights Blinking
Complete image segmentation efficiently based on MindSpore and realize Dice!
High-quality DeFi application building guide to help developers enjoy DeFi Summer
数据可视化(一)
The founder of the DFINITY Foundation talks about the ups and downs of the bear market, and where should DeFi projects go?
[Unity] [UGUI] [Display text on the screen]
uniapp 连接ibeacon
Score interview (1)----related to business
Ali's new launch: Microservices Assault Manual, all operations are written out in PDF
RT - Thread record (a, RT, RT Thread version - Thread Studio development environment and cooperate CubeMX quick-and-dirty)
静态链接和动态链接