Realloc 与链表扫描

发布于 2024-11-07 01:46:09 字数 256 浏览 3 评论 0原文

我必须从文件中读取未知数量的行并将它们保存到结构中(我想避免预处理来计算元素总数)。 在阅读阶段之后,我必须对这些行的每个元素进行一些计算。

我想出了两种方法:

  1. 每次读取一行时都使用realloc。这样,分配阶段很慢,但由于索引访问,计算阶段更容易。

  2. 每次读取一行时都使用链表。这样分配阶段会更快,但计算阶段会更慢。

从复杂性的角度来看,什么更好?

I have to read from a file an unknown number of rows and save them in to a structure (I would like to avoid a prepocessing to count the total number of elements).
After the reading phase I have to make some computations on each of the elements of these rows.

I figured out two ways:

  1. Use realloc each time I read a row. This way the allocation phase is slow but the computation phase is easier thanks to the index access.

  2. Use a linked list each time I read a row. This way the allocation phase is faster but the computation phase is slower.

What is better from a complexity point of view?

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

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

发布评论

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

评论(3

断舍离 2024-11-14 01:46:09

您将多久遍历一次链表?如果只有一次,就使用链表。另外一些事情:会有很多小额分配吗?您可以创建一些较小的缓冲区(假设有 10 行)并将它们链接在一起。但这都是一个分析问题。

我会先做最简单的事情,看看是否符合我的需求,然后我才会考虑优化。

有时,即使第二最佳解决方案也完全满足需求,人们也会浪费太多时间考虑最佳方案。

How often will you traverse the linked list? If it's only once go for the linked-list. Another few things: vill there be a lot of small allocations? You could make a few smaller buffers for let's say 10 lines and link those togeteher. But that's all a question of profiling.

I'd do the simplest thing first and see if that fits my needs only then i'd think about optimizing.

Sometimes one wastes too much time thinking about the optimum even when the second best solution also fits the needs perfectly.

揪着可爱 2024-11-14 01:46:09

如果没有关于如何使用这些信息的更多详细信息,就很难对其复杂性进行评论。但是,这里有一些想法:

  • 如果您使用 realloc,那么最好通过 realloc 添加“一些”更多项目(而不是每次都添加一个)。通常,一个好的算法是每次将大小加倍。
  • 如果您使用链接列表,则可以通过简单的后处理步骤来加快访问速度。分配一个指向项目的指针数组,并在将数组元素设置为列表中的每个项目后遍历列表。
  • 如果文件中的项目具有固定大小,您可以简单地通过查找文件末尾、确定大小、除以项目大小来预先计算大小,即可得到结果。即使它不是固定大小,您也可以使用它作为估计值来“接近”必要的大小并减少所需的重新分配数量。

Without more details on how you are going to use the information, it is a bit tough to comment on the complexity. However, here are a few thoughts:

  • If you use realloc, it would likely be better to realloc to add "some" more items (rather than one each and every time). Typically, a good algorithm is to double the size each time.
  • If you use a linked list, you could speed up the access in a simple post-processing step. Allocate an array of pointers to the items and traverse the list once setting the array elements to each item in the list.
  • If the items are of a fixed size in the file, you could pre-compute the size simply by seeking to the end of the file, determining the size, divide by the item size and you have the result. Even if it is not a fixed size, you could possibly use this as an estimate to get "close" to the necessary size and reduce the number of reallocs required.
时光瘦了 2024-11-14 01:46:09

正如其他用户已经说过的:

过早优化是问题的根源
一切邪恶

Donald Knuth

我有一个使用 realloc 的不同建议:在 C++ STL 中,每次插入对象并且没有足够的可用空间时,std::vector 容器都会增长。增长的大小取决于当前预分配的大小,但特定于实现。例如,您可以保存预分配对象的实际数量。如果大小用完,您可以使用当前分配的双倍空间量调用reallocate。我希望这是可以理解的!

当然,需要注意的是,您可能会分配比实际消耗和需要更多的空间。

as other users already have stated:

Premature optimization is the root of
all evil

Donald Knuth

I have a different proposal using realloc: in the C++ STL the std::vector container grows every time an object is inserted and not enough space is available. The size of the growing depends on the current pre-allocated size but is implementation specific. For example, you could save the actual number of preallocated objects. If the size runs out, you call reallocate with the double amount of space as currently allocated. I hope this was somewhat understandable!

The caveeat is of course, that you propably will allocate more space than you actually will consume and need.

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