C性能 [英] C perfomance

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

问题描述

a)前期与后期增加/减少


我读过某个地方:


?喜欢预增量和减少到后缀运算符。 postfix

运算符(i ++)将现有值复制到临时对象,增加

的内部值,然后返回临时值。前缀运算符

(++ i)递增值并返回对它的引用。对于诸如迭代器之类的对象

,与

内置的int相比,创建临时副本是昂贵的。?


现代编译器不会进行优化,所以


i ++;





++ i ;


会给出相同的指示吗?

b)我发现realloc()调用有时需要更多时间来完成比

malloc()调用。这是一般情况吗?

c)为什么有些人在每个

函数的开头声明所有变量?我的意思是所有变量,包括那些嵌套在

深度fors和ifs中的变量......我没有看到任何明显的性能提升 - 除非

他们这样做记住他们正在使用的......

a) pre vs post increment/decrement

I have read somewhere that:

?Prefer pre-increment and -decrement to postfix operators. Postfix
operators (i++) copy the existing value to a temporary object, increment
the internal value, and then return the temporary. Prefix operators
(++i) increment the value and return a reference to it. With objects
such as iterators, creating temporary copies is expensive compared to
built-in ints.?

A modern compiler wouldn?t make the optimization, so

i++;

and

++i;

would give the same instructions?
b) I find that realloc() calls sometimes take more time to complete than
malloc() calls. Is this the general case?
c) Why do some people declare all the variables at the start of each
function? And I mean ALL variables, including those that are nested in
deep fors and ifs... I don?t see any obvious performance gains - unless
they do it to remember what they are using...

推荐答案

Papadopoulos Giannis写道:

(snip)
Papadopoulos Giannis wrote:
(snip)

c)为什么有些人在每个
函数的开头声明所有变量?我的意思是所有变量,包括那些嵌入在深层次和ifs中的变量......

c) Why do some people declare all the variables at the start of each
function? And I mean ALL variables, including those that are nested in
deep fors and ifs...




有可能有些人不可能知道他们可以在每个*块*的开头声明

变量吗? - )


布鲁诺



Could it be possible that some people don''t know that they can declare
variables at the start of each *block* ?-)

Bruno


Bruno Desthuilliers写道:
Bruno Desthuilliers wrote:
Papadopoulos Giannis写道:
(剪辑)
Papadopoulos Giannis wrote:
(snip)

c)为什么有些人在每个
函数的开头都声明了所有变量吗?我的意思是所有变量,包括那些嵌入在深层次和ifs中的变量......

c) Why do some people declare all the variables at the start of each
function? And I mean ALL variables, including those that are nested in
deep fors and ifs...



有可能有些人不知道他们可以在每个*块*的开头声明
变量? - )

Bruno


Could it be possible that some people don''t know that they can declare
variables at the start of each *block* ?-)

Bruno



也许..但我经常发现它,我想知道。 ..


Maybe.. But I find it often and I wonder...


在文章< bv *********** @ ulysses.noc.ntua.gr>中,
Papadopoulos Giannis< ip ****** @ inf.uth.gr>写道:
In article <bv***********@ulysses.noc.ntua.gr>,
Papadopoulos Giannis <ip******@inf.uth.gr> wrote:
a)前期与后期增加/减少

我在某处读过:

3预先增加和 - 减少后缀运算符。 Postfix
运算符(i ++)将现有值复制到临时对象,增加内部值,然后返回临时值。前缀运算符
(++ i)递增值并返回对它的引用。对于诸如迭代器之类的对象,与
内置的int相比,创建临时副本是昂贵的.2


我打赌你没看过关于C.的书。

b)我发现realloc()调用有时比完成malloc()调用需要更多的时间来完成。这是一般情况吗?
c)为什么有些人在每个
函数的开头声明所有变量?而且我的意思是所有变量,包括那些嵌入在深层次和ifs中的变量......我没有看到任何明显的性能提升 - 除非他们这样做以记住他们正在使用的......
a) pre vs post increment/decrement

I have read somewhere that:

3Prefer pre-increment and -decrement to postfix operators. Postfix
operators (i++) copy the existing value to a temporary object, increment
the internal value, and then return the temporary. Prefix operators
(++i) increment the value and return a reference to it. With objects
such as iterators, creating temporary copies is expensive compared to
built-in ints.2
I bet you didn''t read that in a book about C.
b) I find that realloc() calls sometimes take more time to complete than
malloc() calls. Is this the general case? c) Why do some people declare all the variables at the start of each
function? And I mean ALL variables, including those that are nested in
deep fors and ifs... I don1t see any obvious performance gains - unless
they do it to remember what they are using...




你太担心性能,而且你太担心

错误的性能。首先尝试编写无错误的代码和

可读。这是最重要的事情。


如果需要让你的代码更快:首先衡量。给自己一个

的探查器,学习如何使用它,学习如何解释这些数字。然后

然后试图弄清楚如何更快地进行操作

a $ times,弄清楚如何只做100,000次或1000

次。这就是你如何快速制作节目。



You worry too much about performance, and you worry too much about the
wrong kind of performance. First try to write code that is bug-free and
readable. That is the most important thing.

If there is need to make your code faster: First measure. Get yourself a
profiler, learn how to use it, learn how to interpret the numbers. Then
before trying to figure out how to make an operation faster that you do
a million times, figure out how to do it only 100,000 times or 1000
times. That''s how you make a program fast.


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

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