gpt4 book ai didi

c - 在 VS 和 Xcode 中重置数组有奇怪的时间戳差异

转载 作者:行者123 更新时间:2023-11-30 17:10:45 24 4
gpt4 key购买 nike

编辑 (09/24/15):在 Ubuntu 时间戳上添加了 GCC

我有一个 17 维数组(恰好包含 79,626,240 个值),每个循环都需要将其重置(到 -3)。

我编写了一段代码来测试使用 memset 和 for 循环重置此数组,并记录重置数组所需的平均时间。比较 Xcode 和 Visual Studio 中的相同代码块,出现一些非常奇怪的结果...

这是我的时间戳(代码附加在本文的底部):

                   | XCODE 7.0  |  VS 14     |  GCC      |
---------------------------------------------------------
memset (seconds) | 0.00450 s | 0.00719 s | 0.01197 s |
for-loop (seconds) | 0.73300 s | 0.00728 s | 1.08112 s |

时间不一致是怎么回事?为什么 memset 比 Xcode 中使用 for 循环快两个数量级,但在 Visual Studio 中,它们在功能上花费的时间完全相同?

代码如下。 compiletable_main 使用 for 循环来重置数组,而 compiletable_main_3 使用 memset。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <memory.h>

#define num_runs 10
#define num_gens 4000

#define num_threads 3

//// BEGIN CATABLE STRUCTURE
#define box1 3
#define box2 3
#define box3 3
#define box4 3

#define box5 4
#define box6 4
#define box7 4
#define box8 4

#define memvara 2
#define memvarb 2
#define memvarc 2
#define memvard 2

#define tdirect 1
#define adirect 4
#define outputnum 15
#define fs 2
#define bs 2

typedef struct calookup
{

signed char n[box1][box2][box3][box4][box5][box6][box7][box8][memvara][memvarb][memvarc][memvard][adirect][tdirect][fs][bs][outputnum];


} calookup;

//// END CATABLE STRUCTURE

int ra_pos = 0;
long int dimensions = box1*box2*box3*box4*box5*box6*box7*box8*memvara*memvarb*memvarc*memvard*adirect*tdirect*fs*bs*outputnum;


// Compiletable_main
void compiletable_main(calookup *lookup) {

int i, j, k, l, m, nb, o, p, na, nx, ny, nx1, naa, nbb;
int x, y, z, zz, xa, xb, xc, xd, ncc, ndd;


for (j = 0;j < box1; j++)
{
for (k = 0; k < box2; k++)
{
for (l = 0; l < box3; l++)
{
for (m = 0; m < box4; m++)
{
for (x = 0;x < box5; x++)
{
for (y = 0; y < box6; y++)
{
for (xa = 0;xa < box7; xa++)
{
for (xb = 0; xb < box8; xb++)
{
for (nb = 0; nb < memvara; nb++)
{
for (na = 0; na < memvarb; na++)
{
for (nx = 0; nx < memvarc; nx++)
{
for (nx1 = 0; nx1 < memvard; nx1++)
{
for (naa = 0; naa < adirect; naa++)
{
for (nbb = 0; nbb < tdirect; nbb++)
{
for (ncc = 0; ncc < fs; ncc++)
{
for (ndd = 0; ndd < bs; ndd++)
{
for (o = 0; o < outputnum; o++)
{
lookup->n[j][k][l][m][x][y][xa][xb][nb][na][nx][nx1][naa][nbb][ncc][ndd][o] = -3; //set to default value
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}

void compiletable_main_3(calookup *lookup) {

memset(lookup->n, -3, (dimensions*sizeof(signed char)));

}

void evaluatepopulation_tissueb(calookup *rs) {

// Swap between compiletable_main_3 and compiletable_main
// for the memset and for-loop approaches, respectively

compiletable_main_3(rs);

}

calookup ra;

int main() {

printf(" Begin program... \n");

static double time_consumed = 0;
static double avg_time = 0;
clock_t start, end;

int i;
int i_max = 100;

start = clock();

for (i = 0; i < i_max; i++) {

evaluatepopulation_tissueb(&ra);
}

end = clock();

time_consumed = (double)(end-start)/CLOCKS_PER_SEC;
avg_time = time_consumed / i_max;

printf("Completed run \n");
printf(" Total Time : %lf \n ", time_consumed);
printf(" Avg Time/Loop : %lf \n", avg_time);

//sleep(70000);

return 0;

}

最佳答案

C 拥有一长串由不同组织和个人开发的编译器 check here

这些编译器在优化和许多其他方面采用的技术差异很大。 Visual Studio 和 XCode 来自不同的科技巨头,您不应期望它们使用相同的编译器。

基本的谷歌搜索显示苹果支持 LLVM/Clang,它可以完全替代 GCC

微软使用的是不同的。因此,您可能会注意到总花费时间的差异。尝试在 GCC 或 TCC 上运行您的代码!您应该再次得到不同的答案。

您上传的代码在 GCC 上的结果:

Notice Different Answers

关于c - 在 VS 和 Xcode 中重置数组有奇怪的时间戳差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32754058/

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