malloc() - 性能? [英] malloc() -- Performance?

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

问题描述

什么时候应该使用malloc()和相关函数(例如calloc(),realloc())




我一直听到保留东西尽可能地内存不足,因为

它不像寄存器中的东西那么快。


Brian

When should malloc() and related functions (e.g. calloc(), realloc() )
be used?

I keep hearing to keep stuff out of memory as much as possible since
it''s not as fast as when stuff is in the registers.

Brian

推荐答案

在文章< IppGc.10431
In article <IppGc.10431


151.9250@fed1read02>, xb ********** @ cox.net 说...
151.9250@fed1read02>, xb**********@cox.net says...
什么时候应该malloc()和相关函数(例如calloc(),realloc())
是否会被使用?

我一直听到要尽可能地保留内存,因为它不是很快就像寄存器中的东西一样。
When should malloc() and related functions (e.g. calloc(), realloc() )
be used?

I keep hearing to keep stuff out of memory as much as possible since
it''s not as fast as when stuff is in the registers.




根据你的问题,你最好的选择是不要担心

关于它。现代编译器非常清楚知道何时使用CPU寄存器来获得性能以及何时不使用。


稍后,如果您有特定需求尝试在编译器中超越优化器

,这可能会改变。


-

Randy Howard

要回复,请删除FOOBAR。



Based upon your question, your best bet at this point is to not worry
about it. Modern compilers do a very good job of knowing when to
use CPU registers for performance and when not to.

Later on, if you have a specific need to try and outrun the optimizer
in your compiler, that may change.

--
Randy Howard
To reply, remove FOOBAR.


On Mon,05 Jul 2004 20:54:13 -0700,tweak< xb ******* ***@cox.net>在comp.lang.c中写了


On Mon, 05 Jul 2004 20:54:13 -0700, tweak <xb**********@cox.net> wrote
in comp.lang.c:
什么时候应该malloc()和相关函数(例如calloc(),realloc())
使用?


当你需要动态分配
内存时,应该使用这些函数。一个原因是当你不知道运行时间之前的数据量。

我一直听到要尽可能地保留内存,因为
它不像寄存器中的东西那么快。


你一直听到谁的声音?当然不是来自任何有信誉的

来源关于C.

Brian
When should malloc() and related functions (e.g. calloc(), realloc() )
be used?
These function should be used when you need to dynamically allocate
memory. One reason is when you do not know the amount of data until
run time.
I keep hearing to keep stuff out of memory as much as possible since
it''s not as fast as when stuff is in the registers.
You keep hearing this from whom? Certainly not from any reputable
source about C.
Brian




曾几何时最流行的电脑在世界上是

Apple II,8位6502处理器,运行频率约为1 MHz,原装TRS-80原装TRS-80,运行8位Z80处理器1.77 MHz。

这些处理器都不能执行1,000,000个8位指令
平均每秒b $ b。


今天,桌子顶级处理器的时钟速度超过3 GHz,每个时钟周期平均有超过一个32位或64位指令。

它们至少有5个数量级比20年前的

计算机更好的性能。


这时你需要开始考虑寄存器与内存,
$满足所有这些条件后b $ b:


1.你对C的了解远远超过今天。


2你已经制作了一个程序在所有方面都是正确的,

它满足了它的所有要求,除了没有足够快地执行




3.您已经确认无法对您的

计划进行进一步的改进,无论是选择更有效的算法还是在C中编码它都会更有效。


4.你已经对你的程序进行了描述并证明了内存带宽是瓶颈。


然后,只有然后,你应该开始担心寄存器与

内存。


-

Jack Klein

主页: http://JK-Technology.Com



comp.lang.c的常见问题解答 http://www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ htt p://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a ... FAQ-acllc.html



Once upon a time the most popular computers in the world were the
Apple II with an 8-bit 6502 processor running at about 1 MHz and the
original TRS-80 with an 8-bit Z80 processor running at 1.77 MHz.
Neither of these processors could execute 1,000,000 8-bit instructions
per second on average.

Today, desk top processors are exceeding 3 GHz clock speeds, and
averaging more than one 32-bit or 64-bit instruction per clock cycle.
They have at least 5 orders of magnitude greater performance than the
computers of 20 years ago.

Here''s when you need to start thinking about registers versus memory,
after you meet all of these conditions:

1. You know a lot more about C than you do today.

2. You have produced a program that is correct in all respects, that
it that it meets all of its requirements except for failing to execute
fast enough.

3. You have verified that no further improvements can be made to your
program in terms of selecting a more efficient algorithm or coding it
more efficiently in C.

4. You have profiled your program and proved that memory bandwidth is
the bottle neck.

Then, and only then, should you start worrying about registers versus
memory.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


这篇关于malloc() - 性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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