free()性能问题 [英] free() performance question

查看:78
本文介绍了free()性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的代码中,我需要将两个缓冲区连成一个。我采取以下步骤

步骤:

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?

推荐答案

Lianjun Jiang< li ***** *@gmail.comwrites:
Lianjun Jiang <li******@gmail.comwrites:

在我的代码中,我需要将两个缓冲区连成一个。我采取以下步骤

步骤:

1 malloc新空间

2复制两个缓冲区,

3。然后释放原始的两个缓冲区。

我在每一步都放了计时器。而且我发现在一些压力下(bpu密集,使用了大量内存),最后一步比第1步和第2步花了更多的时间。有没有人知道什么可能是

的原因?
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) ks *** @ mib.org < 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"


00年6月28日:03,Lianjun Jiang< lianj ... @ gmail.comwrote:
On 28 Jun, 00:03, Lianjun Jiang <lianj...@gmail.comwrote:



在我的代码中,我需要连续两个缓冲区合二为一。我采取以下步骤

步骤:

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?



我不知道为什么免费需要很长时间但是一个建议

是你使用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 ?


6月27日,下午4:58,Spiros Bousbouras< spi ... @ gmail.comwrote:
On Jun 27, 4:58 pm, Spiros Bousbouras <spi...@gmail.comwrote:

6月28日00:03,Lianjun Jiang< lianj .. 。@ gmail.comwrote:
On 28 Jun, 00:03, Lianjun Jiang <lianj...@gmail.comwrote:



在我的代码中,我需要将两个缓冲区连成一个。我采取以下步骤

步骤:

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?



我不知道为什么免费需要很长时间但是一个建议

是你使用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()性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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