问题描述
在我的代码中,我需要将两个缓冲区连成一个。我采取以下步骤
步骤:
1 malloc新空间
2复制两个缓冲区,
3。然后释放原始的两个缓冲区。
我在每一步都放了计时器。而且我发现在一些压力下(bpu密集,使用了大量内存),最后一步比第1步和第2步花了更多的时间。有没有人知道可能是什么原因?
Hi,
In my code, I need to concat two buffer into one. I take following
steps:
1 malloc a new space
2 copy two buffers over,
3. and then free the originial two buffers.
I put timer on each step. And I found that under some stress
condition(cpu intensive, lots of memory used), the last step took a
lot more time than step 1 and 2. Did anyone know what could be the
reason?
推荐答案
这是非常特定于系统的。
C标准对malloc的性能一无所知/>
和免费(或其他任何东西,就此而言)。
malloc和free必须操纵任何内部数据结构
实现用于内存分配。有可能
malloc()只需找到至少
所需大小的第一个可用空闲块,但free()必须做大量工作
重新排列事物(合并相邻的免费块,更新统计数据,
无论如何)以用于将来的分配。但那是对我的
部分的纯粹猜测。
-
Keith Thompson(The_Other_Keith)< http://www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
- Antony Jay和Jonathan Lynn,是部长
It''s extremely system-specific.
The C standard says absolutely nothing about the performance of malloc
and free (or of anything else, for that matter).
malloc and free have to manipulate whatever internal data structures
the implementation uses for memory allocation. It''s possible that
malloc() merely has to find the first available free chunk of at least
the requested size, but free() has to do considerable work to
rearrange things (merging adjacent free chunks, updating statistics,
whatever) for future allocations. But that''s pure speculation on my
part.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
我不知道为什么免费需要很长时间但是一个建议
是你使用realloc来使第一个缓冲区变大/>
足以适应第二个缓冲区,然后将
秒缓冲区添加到第一个缓冲区的末尾并释放第二个缓冲区。
也许这会运行更快。
您确定您使用的计时器是否可靠?
I have no idea why free takes a long time but one suggestion
is that you use realloc to make the first buffer large
enough to also fit the second buffer , then add the
second buffer to the end of the first and free the second.
Perhaps this will run faster.
Are you sure that the timer you''re using is reliable ?
我不知道为什么免费需要很长时间但是一个建议
是你使用realloc来使第一个缓冲区变大/>
足以适应第二个缓冲区,然后将
秒缓冲区添加到第一个缓冲区的末尾并释放第二个缓冲区。
也许这会运行更快。
您确定您使用的计时器是否可靠?
I have no idea why free takes a long time but one suggestion
is that you use realloc to make the first buffer large
enough to also fit the second buffer , then add the
second buffer to the end of the first and free the second.
Perhaps this will run faster.
Are you sure that the timer you''re using is reliable ?
首先,我的实现是使用realloc,然后realloc需要
与免费相似的时间。我认为这是因为realloc也需要一个免费的
电话。所以我使用了当前的方法,并且确认它是免费的,这可以减慢realloc。
对于定时器,它至少提供非常一致的结果。
At first, my implementation is using realloc, and then realloc takes
similar time as free. I think that is because realloc need one free
call, too. So I used current approach, and kind of verify it is the
free that slow down realloc.
For the timers, it at least provides very consistent result.
这篇关于free()性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!