哪一种使用C数组中的数据组织使得最快的code,为什么? [英] Which kind of data organization using C arrays makes fastest code and why?

查看:127
本文介绍了哪一种使用C数组中的数据组织使得最快的code,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于下面的数据,什么是组织元素的数组,这样最快的随机访问将是最好的方式?

Given following data, what is the best way to organize an array of elements so that the fastest random access will be possible?

每一个元素都有一些整型数字,在年底的3个字符用'\\ 0'的名字,和一个浮点值

我看到两种可能的方法来组织和访问权限,例如数组:

I see two possible methods to organize and access such array:

首先:

typedef struct { int num; char name[4]; float val; } t_Element;
t_Element array[900000000];
//random access:
num = array[i].num;
name = array[i].name;
val = array[i].val;
//sequential access:
some_cycle:
  num = array[i].num
  i++;

二:

#define NUMS 0
#define NAMES 1
#define VALS 2
#define SIZE (VALS+1)
int array[SIZE][900000000];
//random access:
num = array[NUMS][i];
name = (char*) array[NAMES][i];
val = (float) array[VALS][i];
//sequential access:
p_array_nums = &array[NUMS][i];
some_cycle:
  num = *p_array_nums;
  p_array_nums++;  

我的问题是,用什么方法比较快,为什么?我首先想到的是第二种方法,使最快的code,并允许最快块拷贝,但我怀疑它是否节省了CPU指令任何敏感数相比,第一种方法?

My question is, what method is faster and why? My first thought was the second method makes fastest code and allows fastest block copy, but I doubt whether it saves any sensitive number of CPU instructions in comparison to the first method?

推荐答案

这取决于通用访问模式。如果您打算遍历数据,访问每一个元素,你去了,结构办法是更好的。如果你计划通过每个组件独立地迭代,然后并行阵列是更好的。

It depends on the common access patterns. If you plan to iterate over the data, accessing every element as you go, the struct approach is better. If you plan to iterate independently over each component, then parallel arrays are better.

这是不是一个微妙的区别,无论是。与主存储器典型地围绕两个数量比L1高速缓存大小较慢的,使用数据结构,是适合于使用模式都不可能三重性能

This is not a subtle distinction, either. With main memory typically being around two orders of magnitude slower than L1 cache, using the data structure that is appropriate for the usage pattern can possibly triple performance.

我必须说,虽然,你的方法来实现并行阵列极不理想。你应该简单地声明,而不是越来越聪明用二维数组和铸造三个数组:

I must say, though, that your approach to implementing parallel arrays leaves much to be desired. You should simply declare three arrays instead of getting "clever" with two-dimensional arrays and casting:

int nums[900000000];
char names[900000000][4];
float vals[900000000];

这篇关于哪一种使用C数组中的数据组织使得最快的code,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆