优化问题 [英] Optimiser question

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

问题描述

大家好,

我已经获得了一些代码。它在很大程度上依赖于

优化器以正确的速度运行。考虑到这一点,我一直在浏览它,看看我能不能帮助它。我已经限制了

关于编译器如何工作的知识,但我知道一些

构造更容易/更好。我们在Nios II平台上使用gcc

(我认为是3.4.1)的变体,如果这有所不同。

我的问题是这2个片段在功能上相同(我认为他们是这样),并且优化者会更喜欢第二个:

第一个片段,就像现在一样:

while(count - > 0)

{

* pDest ++ = nPattern;

}


我如何建议''改进''代码:


while(count> 0)

{

* pDest = nPattern;

++ pDest;

--count;

}


有很多位有点像这样,所以如果我是正确的话,这可能是一个很大的帮助。


欢呼


Dave

解决方案

Dave S schrieb:


我的问题是这2个片段在功能上是相同的(我认为它们是b $ b),并且会他的优化者更喜欢第二个:



说真的,你为什么不试试呢?编译两段代码,

对它们执行objdump -d,看看汇编输出。没有人

但你可以给你一个确切的答案,考虑到

你正在使用不寻常的平台-3.4.1-i-think-gcc作为编译。


问候,

约翰内斯


-

" Viele der Theorien der Mathematiker sind falsch und klar

Gottesl?¤sterlich。 Ich vermute,dass diese falschen Theorien genau

deshalb so geliebt werden。 - Prophet und Vision?¤rHansJoss aka

HJP in de.sci.mathematik< 47 ********************* *@news.sunrise.ch>


Dave S写道:


大家好,

我已经获得了一些代码。它在很大程度上依赖于

优化器以正确的速度运行。考虑到这一点,我一直在浏览它,看看我能不能帮助它。我已经限制了

关于编译器如何工作的知识,但我知道一些

构造更容易/更好。我们在Nios II平台上使用gcc

(我认为是3.4.1)的变体,如果这有所不同。

我的问题是这2个片段在功能上相同(我认为他们是这样),并且优化者会更喜欢第二个:

第一个片段,就像现在一样:

while(count - > 0)

{

* pDest ++ = nPattern;

}


我如何建议''改进''代码:


while(count> 0)

{

* pDest = nPattern;

++ pDest;

--count;

}



您可以通过以下两种方式调查此问题:



b)检查生成的代码


我不是专家,但我怀疑你会发现这么简单的改变

会带来很大的好处。


一般来说,我怀疑你会更好地使用更高的o来看待

a)编译器上的优化级别

b)看看最近的编译器构建是否有更好的优化

c)查看你的算法(而不是你的代码)


Dave S写道:


大家好,

我已经获得了一些代码。它在很大程度上依赖于

优化器以正确的速度运行。考虑到这一点,我一直在浏览它,看看我能不能帮助它。我已经限制了

关于编译器如何工作的知识,但我知道一些

构造更容易/更好。我们在Nios II平台上使用gcc

(我认为是3.4.1)的变体,如果这有所不同。

我的问题是这2个片段在功能上相同(我认为他们是这样),并且优化者会更喜欢第二个:

第一个片段,就像现在一样:

while(count - > 0)

{

* pDest ++ = nPattern;

}


我如何建议''改进''代码:


while(count> 0)

{

* pDest = nPattern;

++ pDest;

--count;

}



第一个片段更具惯用性C,所以优于第二个优化的优惠价格。


如果是程序跑得不够快,而且你想跑得更好

走得更快,你/必须/做的第一件事就是找出

时间的去向/ 。为了上帝的缘故,不要只是通过程序搜索

寻找你认为可以帮助的位:/找出/哪些位

正在吃时间平台

可用的任何分析工具。找出哪些是慢速位并进行一个/ b $ b / b算法/改进将获得更好的结果。即使只是找到缓慢的位并改进代码也会有所帮助。


你不知道程序是做什么的,所以很难猜猜是什么

可能是循环汇,但如果它有任何字符串黑客攻击,请记住

,`strlen`和`strcat`(作为例子)不是常数 - 时间

操作...


-

克里斯谁知道时间到了什么地方? Dollin


Hewlett-Packard Limited Cain Road,Bracknell,注册号:

注册办事处:Berks RG12 1HN 690597 England


Hi All,
I have been given some code to wok on. It relies heavily on the
optimiser to run at the correct speed. With this in mind I have been
loking through it to see if I can help it out a bit. I have limitied
knowledge of how compilers works, but I understand that some
constructs optimise easier / better. We are using a variant of gcc
(3.4.1 I think) on Nios II platform, if that makes a difference.
The question I have is are these 2 snippets functionally the same( I
think they are), and would the optimiser prefer the second one:
First snippet, as it is now:

while(count-->0)
{
*pDest++=nPattern;
}

how I propose to ''improve'' the code:

while(count>0)
{
*pDest=nPattern;
++pDest;
--count;
}

There are numerous bits a bit like this, so this would probably be a
big help overall if I am correct.

cheers

Dave

解决方案

Dave S schrieb:

The question I have is are these 2 snippets functionally the same( I
think they are), and would the optimiser prefer the second one:

Seriously, why don''t you just try it out? Compile both pieces of code,
do an objdump -d on them as take a look at the assembly output. Nobody
but you will be able to give you a precise answer considering that
you''re using unusual-platform-3.4.1-i-think-gcc as a compiler.

Greetings,
Johannes

--
"Viele der Theorien der Mathematiker sind falsch und klar
Gottesl?¤sterlich. Ich vermute, dass diese falschen Theorien genau
deshalb so geliebt werden." -- Prophet und Vision?¤r Hans Joss aka
HJP in de.sci.mathematik <47**********************@news.sunrise.ch>


Dave S wrote:

Hi All,
I have been given some code to wok on. It relies heavily on the
optimiser to run at the correct speed. With this in mind I have been
loking through it to see if I can help it out a bit. I have limitied
knowledge of how compilers works, but I understand that some
constructs optimise easier / better. We are using a variant of gcc
(3.4.1 I think) on Nios II platform, if that makes a difference.
The question I have is are these 2 snippets functionally the same( I
think they are), and would the optimiser prefer the second one:
First snippet, as it is now:

while(count-->0)
{
*pDest++=nPattern;
}

how I propose to ''improve'' the code:

while(count>0)
{
*pDest=nPattern;
++pDest;
--count;
}

The two ways you could investigate this are
a) measuring
or
b) examining the generated code

I''m no expert, but I doubt that you will find that such simple changes
would reap significant benefits.

In general, I would suspect you''d be better off looking at
a) using a higher optimisation level on the compiler
b) seeing if a recent compiler build has better optimisation
c) looking at your algorithms (rather than your code)


Dave S wrote:

Hi All,
I have been given some code to wok on. It relies heavily on the
optimiser to run at the correct speed. With this in mind I have been
loking through it to see if I can help it out a bit. I have limitied
knowledge of how compilers works, but I understand that some
constructs optimise easier / better. We are using a variant of gcc
(3.4.1 I think) on Nios II platform, if that makes a difference.
The question I have is are these 2 snippets functionally the same( I
think they are), and would the optimiser prefer the second one:
First snippet, as it is now:

while(count-->0)
{
*pDest++=nPattern;
}

how I propose to ''improve'' the code:

while(count>0)
{
*pDest=nPattern;
++pDest;
--count;
}

The first snippet is more idiomatically C and so no less likely to
be optimised than the second.

If the program isn''t running fast enough, and you wish to make it
go faster, the first thing you /must/ do is /find out where the
time goes/. For the gods sake don''t just trawl through the program
looking for bits you think you can help with: /find out/ which bits
are eating the time using whatever profiling tools the platform
has available. Finding out which are the slow bits and doing an
/algorithmic/ improvement will get much better results. Even just
finding the slow bits and improving the code will help.

You don''t say what the program does, so it''s hard to guess what
might be cycle-sinks, but if it does any string-hacking, remember
that `strlen` and `strcat` (as examples) are not constant-time
operations ...

--
Chris "who knows where the time goes?" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England


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

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