简体   繁体   English

在静态分配的数组中进行访问VS通过指向静态分配的数组的指针进行访问

[英]Accessing in a statically-allocated array VS accessing via a pointer to a statically-allocated array

I need to store number of strings(they will remain constant and will not be modified), in an array, and access them many times,we want to achieve as fast as possible lookup. 我需要在一个数组中存储多个字符串(它们将保持不变并且不会被修改),并多次访问它们,我们希望实现尽可能快的查找。 Which of the below will give faster access: 以下哪项将提供更快的访问权限:

Approach 1: 方法1:

const char* string_literal[] = {"Test1","Dummy","Dummy","Test2","Test3","Dummy"}; //       storing as string literals
... ... ... ...
... ... ... ...
const char* string_literal1000[]= {"Beta1","Beta2","Beta3"};

Approach 2: 方法二:

const char test1ptr[] = "Test1",
const char test2ptr[] = "Test2",
const char test3ptr[] = "Test3",
const char dummyptr[] = "Dummy",


const char* string_ptr1 [] = {test1ptr,dummyptr,dummyptr,test2ptr,test3ptr,dummyptr}; // storing as pointers
... ... ...
const char* string_ptr1000[] = {"Beta1","Beta2","Beta3"};

Or; 要么; is it Approach1 and Approach 2 will result in same performance? 方法1和方法2会产生相同的效果吗?

Note: 注意:

  1. I will have around 1000 of records(eg string_ptr1,sting_ptr2 etc. or string_literal1,string_literl2 etc.) conating on average 20 strings(eg"test1",test2" etc.. 我将拥有大约1000条记录(例如string_ptr1,sting_ptr2等或string_literal1,string_literl2等),平均记录20条字符串(例如“ test1”,test2”等)。
  2. "Dummy" will appear only with few records. “虚拟”将仅显示很少的记录。

string_literal1000 wouldn't compile. string_literal1000无法编译。

Simple answer: say same performance (since the allocation pattern is identical). 简单的答案:说出相同的性能(因为分配模式相同)。

However it is kind of funny that you want us to compare speeds, while you're not even showing your usage pattern(s). 但是,您甚至没有显示使用模式时,也希望我们比较速度,这很有趣。

Now all that said, I can imagine situations where you could optimize a little with the following pattern: IFF you know the length of the largest entry (they seem rather small), you could optimize the whole table by packing it into a single char[], containing the individual strings aligned and padded at a nice number of bytes (say 16, or 32 for the strings shown in the OP). 现在,我可以想象一下您可以使用以下模式进行一些优化的情况:IFF您知道最大条目的长度(它们看起来很小),可以通过将整个表打包为单个char []来优化整个表。 ],其中包含对齐并填充的单个字符串,且字节数不错(例如,OP中显示的字符串为16或32)。

Without further info on the actual code scenario's, it would be wrong to recommend such an approach (IMO). 如果没有有关实际代码方案的更多信息,推荐这种方法(IMO)将是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 指向多维静态分配数组部分的指针的语法 - Syntax for pointer to portion of multi-dimensional statically-allocated array 对静态分配的子类对象进行静态分配的纯虚拟父类引用是否合法? - Is it legal to have statically-allocated pure-virtual-parent-class references to statically-allocated child class objects? C ++模板化,静态分配的数组 - C++ templated, statically allocated array 使用Thrust排序静态分配的数组 - Sorting statically allocated array using Thrust 检查指针是否指向静态分配的对象? - Check if a pointer points to statically allocated object? 有没有办法在 C++ 中静态初始化动态分配的数组? - Is there a way to statically-initialize a dynamically-allocated array in C++? 是否可以为静态分配的整数数组指定变量作为大小说明符? - Is it possible to specify a variable as size specifier for a statically allocated integer array? C ++动态分配静态大小的数组 - C++ dynamically allocated array of statically dimensioned arrays 正常声明数组时,memory 是静态分配、动态分配还是其他方式分配? - When an array is declared normally, is the memory allocated statically, dynamically or otherwise? 如何创建静态分配的动态大小的数组? - How can I create a statically allocated dynamically sized array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM