gpt4 book ai didi

objective-c - 为什么 `do{} while(0);` 这么快?

转载 作者:太空狗 更新时间:2023-10-30 03:58:48 26 4
gpt4 key购买 nike

我尝试了以下 3 个 for 循环:

#define loop 1000000000
NSDate *start;
NSDate *end;


// 1: empty for loop
start = [NSDate date];
for (NSInteger i = 0; i < loop; i++) {
}
end = [NSDate date];
NSLog(@"time interval: %f", [end timeIntervalSince1970] - [start timeIntervalSince1970]);


// 2: do-while for loop
start = [NSDate date];
for (NSInteger i = 0; i < loop; i++) {
do {
} while (0);
}
end = [NSDate date];
NSLog(@"time interval: %f", [end timeIntervalSince1970] - [start timeIntervalSince1970]);

// 3: @try-@finally for loop
start = [NSDate date];
for (NSInteger i = 0; i < loop; i++) {
@try {
}
@finally {
}
}
end = [NSDate date];
NSLog(@"time interval: %f", [end timeIntervalSince1970] - [start timeIntervalSince1970]);

我在我的 07 款 Macbook Pro 上测试了这三个 for 循环大约 10 亿次。并且我记录了时间戳来计算执行时长。以下是结果:

1: empty for loop => 2.947088 sec
2: do-while for loop => 2.581905 sec
3: @try-@finally for loop => 4.216685 sec

什么? do-while for 循环比空的 for 循环更快!

为什么?


已更新

我在 for 循环中添加了一些额外的代码 (j++):

NSInteger j;
#define loop 1000000000

// 1: empty for loop
j = 0
for (NSInteger i = 0; i < loop; i++) {
j++;
}

// 2: do-while for loop
j = 0;
for (NSInteger i = 0; i < loop; i++) {
do {
j++;
} while (0);
}

// 3: @try-@finally for loop
j = 0;
for (NSInteger i = 0; i < loop; i++) {
@try {
j++;
}
@finally {
}
}

输出:

1: empty for loop => 2.590103 sec
2: do-while for loop => 2.138528 sec
3: @try-@finally for loop => 3.983589 sec

这 3 个比以前的代码快得多,do-while for 循环仍然是最快的。奇怪!

最佳答案

不是。输出在很大程度上取决于所使用的特定编译器以及有效的优化设置。任何称职的编译器都会优化这两个循环。当我编译以下示例代码时,Clang 和 GCC 都完全删除了任何非零优化级别(即 -O1 及以上)的循环:

#include <stdio.h>
#include <stdint.h>
#include <sys/time.h>

#define loop 1000000000

int main(void)
{
struct timeval t1, t2, t3;
gettimeofday(&t1, NULL);

for (long i = 0; i < loop; i++) {
}

gettimeofday(&t2, NULL);

for (long i = 0; i < loop; i++) {
do {
} while (0);
}

gettimeofday(&t3, NULL);

int64_t d1 = (t2.tv_sec - t1.tv_sec) * 1000000ll + (t2.tv_usec - t1.tv_usec);
int64_t d2 = (t3.tv_sec - t2.tv_sec) * 1000000ll + (t3.tv_usec - t2.tv_usec);

printf("Empty for loop: %lld.%06d\n", d1 / 1000000, (int)(d1 % 1000000));
printf("do-while for loop: %lld.%06d\n", d2 / 1000000, (int)(d2 % 1000000));

return 0;
}

在优化级别 0(-O0,未优化的代码),Clang 和 GCC 生成的代码要么两个循环以相同的速度运行(在实验误差范围内),要么 do-while 循环由于额外的未优化代码,运行速度稍慢。我在 64 位 Mac 上使用 Clang 4.1 和 GCC 4.2.1 的结果:

Clang, 32-bit, -O0:
Empty for loop: 2.632714
do-while for loop: 2.633194

Clang, 64-bit, -O0:
Empty for loop: 2.632078
do-while for loop: 2.632046

Clang, 32-bit, -O1:
Empty for loop: 0.000000
do-while for loop: 0.000000

Clang, 64-bit, -O1:
Empty for loop: 0.000000
do-while for loop: 0.000000

GCC, 32-bit, -O0:
Empty for loop: 2.633221
do-while for loop: 2.633754

GCC, 64-bit, -O0:
Empty for loop: 2.778056
do-while for loop: 2.983421 (!!!)

GCC, 32-bit, -O1:
Empty for loop: 0.000001
do-while for loop: 0.000000

GCC, 64-bit, -O1:
Empty for loop: 0.000000
do-while for loop: 0.000000

关于objective-c - 为什么 `do{} while(0);` 这么快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20078561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com