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

发布于 2024-12-15 01:49:34 字数 886 浏览 0 评论 0原文

我需要在数组中存储多个字符串(它们将保持不变并且不会被修改),并多次访问它们,我们希望实现尽可能快的查找。以下哪一项可以提供更快的访问速度:

方法 1:

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

方法 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"};

或;方法 1 和方法 2 会产生相同的性能吗?

注意:

  1. 我将有大约 1000 条记录(例如 string_ptr1、sting_ptr2 等或 string_literal1、string_literl2 等),平均包含 20 个字符串(例如“test1”、test2”等。
  2. “Dummy”只会出现在很少的记录中。

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:

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?

Note:

  1. I will have around 1000 of records(e.g. string_ptr1,sting_ptr2 etc. or string_literal1,string_literl2 etc.) conating on average 20 strings(e.g."test1",test2" etc..
  2. "Dummy" will appear only with few records.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽袖吟 2024-12-22 01:49:34

string_literal1000 无法编译。

简单的答案:说相同的性能(因为分配模式是相同的)。

然而,有趣的是,您希望我们比较速度,而您甚至没有显示您的使用模式。

现在说了这么多,我可以想象您可以使用以下模式进行一些优化的情况:如果您知道最大条目的长度(它们看起来相当小),您可以通过将其打包到单个 char[ ],包含以大量字节对齐和填充的各个字符串(例如 16 或 32 对于 OP 中显示的字符串)。

如果没有有关实际代码场景的进一步信息,推荐这种方法是错误的(IMO)。

string_literal1000 wouldn't compile.

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).

Without further info on the actual code scenario's, it would be wrong to recommend such an approach (IMO).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文