当前位置:网站首页>【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变量,然后后续的运算都用此新指针。
边栏推荐
- Chapter 5: Multithreaded Communication—wait and notify
- Ali's new launch: Microservices Assault Manual, all operations are written out in PDF
- FPGA: Basic Getting Started Button Controlling LED Lights
- 化繁为简!阿里新产亿级流量系统设计核心原理高级笔记(终极版)
- 电竞、便捷、高效、安全,盘点OriginOS功能的关键词
- A small test of basic grammar, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, basic grammar of go lang and the use of variables EP02
- The query that the user's test score is greater than the average score of a single subject
- 第六章:activiti流程分流判断之排它网关和并行网关
- Go编译原理系列6(类型检查)
- [Android]如何使用RecycleView in Kotlin project
猜你喜欢

NowCoderTOP35-40——持续更新ing

NowCoderTOP35-40 - continuous update ing

化繁为简!阿里新产亿级流量系统设计核心原理高级笔记(终极版)

In-depth understanding of timeout settings for Istio traffic management

一文道清什么是SPL

创建一个 Dapp,为什么要选择波卡?
![[Strong Net Cup 2022] WP-UM](/img/3d/caeab05ddca278af274dbf6e2f8ba1.png)
[Strong Net Cup 2022] WP-UM

Voice-based social software development - making the most of its value

MySQL transactions

PCB布局必知必会:教你正确地布设运算放大器的电路板
随机推荐
PCB layout must know: teach you to correctly lay out the circuit board of the op amp
[Unity] [UGUI] [Display text on the screen]
R语言使用yardstick包的pr_curve函数评估多分类(Multiclass)模型的性能、查看模型在多分类每个分类上的ROC曲线(precision(精准率),R代表的是recall(召回率)
教你本地编译运行一个IDEA插件,在IDEA里聊天、下棋、斗地主!
JS introduction to reverse the recycling business network of learning, simple encryption mobile phone number
Ali's new launch: Microservices Assault Manual, all operations are written out in PDF
SQL Outer Join Intersection, Union, Difference Query
static linking and dynamic linking
FPGA:基础入门按键控制LED灯
MySQL transactions
用户考试分数大于单科科目平均分的查询
气象数据数据处理实例——matlab字符串切割匹配与R语言日期匹配(数据拼接)
poj2935 Basic Wall Maze (2016xynu暑期集训检测 -----D题)
In-depth understanding of timeout settings for Istio traffic management
单片机:温度控制DS18B20
Confessing in the era of digital transformation: Mai Cong Software allows enterprises to use data in the easiest way
FPGA:开发环境Vivado的使用
Go编译原理系列6(类型检查)
STM32+ULN2003 drives 28BYJ4 stepper motor (forward and reverse according to the number of turns)
A small test of basic grammar, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, basic grammar of go lang and the use of variables EP02