关于C / C ++编译器优化,我可以假设什么?(What can I assume about C/C++ compiler optimisations?)
我想知道如何在将遗留代码,库代码或示例代码集成到我自己的代码库时,通过重新哈希源代码来避免浪费我的时间和冒着输入错误的风险。
如果我举一个简单的例子,基于图像处理场景,你可能会明白我的意思。
实际上,我发现我正在集成如下的代码片段:
for (unsigned int y = 0; y < uHeight; y++) { for (unsigned int x = 0; x < uWidth; x++) { // do something with this pixel .... uPixel = pPixels[y * uStride + x]; } }
随着时间的推移,我已经习惯于将不必要的计算移出内部循环,并可能将后缀增量更改为前缀...
for (unsigned int y = 0; y < uHeight; ++y) { unsigned int uRowOffset = y * uStride; for (unsigned int x = 0; x < uWidth; ++x) { // do something with this pixel .... uPixel = pPixels[uRowOffset + x]; } }
或者,我可能会使用指针算术,或者按行...
for (unsigned int y = 0; y < uHeight; ++y) { unsigned char *pRow = pPixels + (y * uStride); for (unsigned int x = 0; x < uWidth; ++x) { // do something with this pixel .... uPixel = pRow[x]; } }
......或按行列...所以我最终得到了类似的东西
unsigned char *pRow = pPixels; for (unsigned int y = 0; y < uHeight; ++y) { unsigned char *pPixel = pRow; for (unsigned int x = 0; x < uWidth; ++x) { // do something with this pixel .... uPixel = *pPixel++; } // next row pRow += uStride; }
现在,当我从头开始编写代码时,我会习惯性地应用我自己的“优化”,但我知道编译器也会做这样的事情:
- 将代码从内部循环移动到外部循环
- 将后缀增量更改为前缀
- 很多其他我不知道的东西
考虑到每次我以这种方式混淆了一段经过测试的代码,我不仅花费了一些时间,而且还冒着引入手指故障或其他任何问题的风险(上面的例子都是简化的)。 我知道“不成熟的优化”以及通过设计更好的算法等来提高性能的其他方法,但对于上述情况,我正在创建构建块,将用于更大的流水线类型的应用程序中, t预测非功能需求可能是什么,所以我只是希望代码在时间限制内尽可能快而且合理(我的意思是我花时间调整代码的时间)。
所以,我的问题是:我在哪里可以找到“现代”编译器普遍支持哪些编译器优化。 我使用的是Visual Studio 2008和2012的混合版本,但有兴趣知道是否与其他替代品有所不同,例如Intel的C / C ++编译器。 任何人都可以在有用的网页链接,书籍或其他参考资料中发现一些见解和/或指向我吗?
编辑
只是为了澄清我的问题
- 我上面展示的优化是简单的例子,而不是完整的列表。 我知道,从性能角度来看,做出那些特定的改变是毫无意义的,因为编译器无论如何都会这样做。
- 我特别寻找关于我正在使用的编译器提供什么优化的信息。
I would like to know how to avoid wasting my time and risking typos by re-hashing source code when I'm integrating legacy code, library code or sample code into my own codebase.
If I give a simple example, based on an image processing scenario, you might see what I mean.
It's actually not unusual to find I'm integrating a code snippet like this:
for (unsigned int y = 0; y < uHeight; y++) { for (unsigned int x = 0; x < uWidth; x++) { // do something with this pixel .... uPixel = pPixels[y * uStride + x]; } }
Over time, I've become accustomed to doing things like moving unnecessary calculations out of the inner loop and maybe changing the postfix increments to prefix ...
for (unsigned int y = 0; y < uHeight; ++y) { unsigned int uRowOffset = y * uStride; for (unsigned int x = 0; x < uWidth; ++x) { // do something with this pixel .... uPixel = pPixels[uRowOffset + x]; } }
Or, I might use pointer arithmetic, either by row ...
for (unsigned int y = 0; y < uHeight; ++y) { unsigned char *pRow = pPixels + (y * uStride); for (unsigned int x = 0; x < uWidth; ++x) { // do something with this pixel .... uPixel = pRow[x]; } }
... or by row and column ... so I end up with something like this
unsigned char *pRow = pPixels; for (unsigned int y = 0; y < uHeight; ++y) { unsigned char *pPixel = pRow; for (unsigned int x = 0; x < uWidth; ++x) { // do something with this pixel .... uPixel = *pPixel++; } // next row pRow += uStride; }
Now, when I write from scratch, I'll habitually apply my own "optimisations" but I'm aware that the compiler will also be doing things like:
- Moving code from inside loops to outside loops
- Changing postfix increments to prefix
- Lots of other stuff that I have no idea about
Bearing in mind that every time I mess with a piece of working, tested code in this way, I not only cost myself some time but I also run the risk that I'll introduce bugs with finger trouble or whatever (the above examples are simplified). I'm aware of "premature optimisation" and also other ways of improving performance by designing better algorithms, etc. but for the situations above I'm creating building-blocks that will be used in larger pipelined type of apps, where I can't predict what the non-functional requirements might be so I just want the code as fast and tight as is reasonable within time limits (I mean the time I spend tweaking the code).
So, my question is: Where can I find out what compiler optimisations are commonly supported by "modern" compilers. I'm using a mixture of Visual Studio 2008 and 2012, but would be interested to know if there are differences with alternatives e.g. Intel's C/C++ Compiler. Can anyone shed some insight and/or point me at a useful web link, book or other reference?
EDIT
Just to clarify my question
- The optimisations I showed above were simple examples, not a complete list. I know that it's pointless (from a performance point of view) to make those specific changes because the compiler will do it anyway.
- I'm specifically looking for information about what optimisations are provided by the compilers I'm using.
原文:https://stackoverflow.com/questions/15584814
最满意答案
为您想添加常用样式的
UIComponents
创建扩展。就像
UIButton
extension UIButton { open override func draw(_ rect: CGRect) { //provide custom style self.layer.cornerRadius = 10 self.layer.masksToBounds = true } }
或者创建一个
UIButton
的子类并提供你想要应用的所有样式,并确保你的按钮从你的自定义类扩展class MyButton : UIButton { override init(frame: CGRect) { super.init(frame: frame) setup() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) setup() } } private func setup() { self.layer.cornerRadius = 10 self.layer.masksToBounds = true }
Create extensions for
UIComponents
you wanna add common style.Like in case of
UIButton
extension UIButton { open override func draw(_ rect: CGRect) { //provide custom style self.layer.cornerRadius = 10 self.layer.masksToBounds = true } }
Or create a subclass of
UIButton
and provide all the styling u wanna apply and make sure your buttons extends from your custom classclass MyButton : UIButton { override init(frame: CGRect) { super.init(frame: frame) setup() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) setup() } } private func setup() { self.layer.cornerRadius = 10 self.layer.masksToBounds = true }
相关问答
更多-
为您想添加常用样式的UIComponents创建扩展。 就像UIButton extension UIButton { open override func draw(_ rect: CGRect) { //provide custom style self.layer.cornerRadius = 10 self.layer.masksToBounds = true } } 或者创建一个UIButton的子类并提供你想要应用的所有样式,并 ...
-
UIImageViews没有启用用户交互。 您应该将其添加到单元格本身或其他UIView中。 UIImageViews don't have user interaction enabled. You should be adding it to the cell itself, or another UIView.
-
最后,问题是我将子视图添加到单元格对象而不是单元格对象的内容视图。 我将按钮代码更改为此并解决了: UIButton *button = (UIButton *)sender; UITableViewCell *cell = (UITableViewCell *)[button superview]; NSIndexPath *indexPath = [[self tableView] indexPathForCell:cell]; The problem, in the end, was that I ...
-
自定义UIButton圆角(Custom UIButton rounded corners)[2023-03-21]
使用自定义图像创建自定义按钮作为普通UIButtons。 然后使用新的UIButton创建UIBarButtonItems作为自定义视图(您可以在UIBarButtonItem类参考中看到如何执行此操作)。 然后,设置视图控制器的navigationItem以将新的UIBarButtonItems作为leftButtonItem,rightButtonItem等等。 Create your custom buttons as normal UIButtons, with your custom image. ... -
无法在自定义UITableView上添加UIButton而不会出现错误(Can't add UIButton on custom UITableView without an error)[2023-11-05]
你需要这样做。 像PetCell一样创建UITableViewCell的子类 将故事板中的单元格类从UITableViewCell更改为PetCell 将按钮链接到PetCell就像使用ViewController一样 You need do like this. create a subclass of UITableViewCell like PetCell change the class of the cell in storyboard from UITableViewCell to PetCel ... -
如何在UITableViewCell中实例化自定义UIButton(How to instantiate a Custom UIButton in UITableViewCell)[2022-03-31]
我猜你会做的事情如下: CoolButton *b = [UIButton ...] 您需要使用CoolButton类创建它: CoolButton *b = [CoolButton ....]; 或者,如果你使用IB,你创建了一个UIButton但是使用了CoolButton的IBOutlet。 您需要将按钮的实际类别拖动到IB CoolButton中,您可以通过选择按钮并点击(我认为)检查器中左侧顶部选项卡的第二个按钮来完成。 I'm going to guess you are doing som ... -
自定义UIScrollView类中的UIButton不响应触摸(UIButton in custom UIScrollView class not responding to touches)[2022-05-30]
添加约束后刷新布局是必须的 override func viewWillAppear(_ animated: Bool) { scrollView.anchor(topLayoutGuide.bottomAnchor, left: view.leftAnchor, bottom: bottomLayoutGuide.topAnchor, right: view.rightAnchor, topConstant: 10, leftConstant: 10, bottomConstant: 10, r ... -
在代码上跳了一下枪,但请考虑以下内容。 添加一个方法,可以在不使用标记的情况下查找显示视图的单元格。 - (NSIndexPath *)indexPathOfSubview:(UIView *)view { while (view && ![view isKindOfClass:[UITableViewCell self]]) { view = view.superview; } UITableViewCell *cell = (UITableViewCell *)v ...
-
突出显示自定义UIButton(Highlighting a custom UIButton)[2023-10-18]
您可以使用手势识别器拦截触摸事件,然后以编程方式将识别器添加到您的所有uibutton中。 例如: // // HighlighterGestureRecognizer.h // Copyright 2011 PathwaySP. All rights reserved. // #import@interface HighlightGestureRecognizer : UIGestureRecognizer { id *beganBu ... -
创建自定义UIButton类(Create Custom UIButton class)[2020-03-03]
执行此操作的最佳方法是使用UIView的tag属性。 ( UIButton是UIView一个子类。) tag只是一个整数,所以它非常适合循环等等。 不用说,它也适用于子类。 这种方法也出现在Apple的许多例子中。 这很方便,因为您不需要保留任何其他变量。 此外,还有非常实用的方法可以通过viewWithTag访问按钮: UIButton *button = (UIButton *)[theSuperView viewWithTag:i]; 例如,我用这种方法实现了一个钢琴键盘。 编写一个标识每个键(或音 ...