小编Eth*_*eal的帖子

内部数组访问比std :: vector访问快得多 - Black Magic?

我已经设置了一个测试程序来比较数组访问性能和std :: vector的性能.我发现了几个类似的问题,但似乎都没有解决我的具体问题.一段时间以来,为什么数组访问速度似乎比矢量访问快6倍,当我在过去读过它们应该是等价的时候,我一直在挠头.事实证明,这似乎是英特尔编译器(v12)和优化(发生在-O1以上的任何事情)的函数,因为我在使用gcc v4.1.2时看到std :: vector的性能更好,而数组只有 gcc v4.4.4的2倍优势.我正在使用Xeon X5355内核的RHEL 5.8机器上运行测试.顺便说一句,我发现迭代器比元素访问更快.

我正在使用以下命令进行编译:

icpc -fast test.cc
g++44 -O3 test.cc
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释速度的显着提高吗?

#include <vector>
#include <iostream>

using namespace std;

int main() {
  int sz = 100;
  clock_t start,stop;
  int ncycle=1000;
  float temp  = 1.1;

  // Set up and initialize vector
  vector< vector< vector<float> > > A(sz, vector< vector<float> >(sz,  vector<float>(sz, 1.0)));

  // Set up and initialize array
  float*** a = new float**[sz];
  for( int i=0; i<sz; ++i) {
    a[i] = new float*[sz];
    for( …
Run Code Online (Sandbox Code Playgroud)

c++ arrays iterator vector intel

6
推荐指数
1
解决办法
1759
查看次数

使用函数调用初始化静态全局数据(在编译时)

我试图通过在编译时计算数字序列并将它们存储为静态向量来节省计算时间(但我现在可能会在运行时开始计算一次)。我正在尝试做的一个简单(非编译)示例是:

#include <vector>
using namespace std;

static vector<vector<int> > STATIC_THING(4, vector<int>(4));

void Generator(int x, int y, vector<int> *output) {
  // Heavy computing goes here
  for(int i=0; i < 4; ++i)
    (*output)[i] = x * y;
  return;
}   
static void FillThings() {
  for(int x=0; x < 4; ++x)
    for(int y=0; y < 4; ++y)
      Generator(x, y, &STATIC_THING[x]);
}   
FillThings();

int main() {
}   
Run Code Online (Sandbox Code Playgroud)

除了预先计算并将我的序列硬编码到数组中之外,还有其他方法可以让编译器解决这个问题吗?我觉得应该有一种方法至少可以在这将存在的头文件的第一个 #include 上完成,但我只看到它用类完成。如果可以在编译时方便计​​算,我可以使用数组而不是向量。

编辑:

  • 尽管建议使用模板元编程,但我的实际生成器算法太复杂了,无法使用这种技术。

  • 使用查找表似乎是我唯一可以避免运行时计算的其他选择;如果性能在未来仍然是一个问题,我将回到这一点。

c++ performance static

4
推荐指数
1
解决办法
2776
查看次数

为什么必须限定重载模板基类方法的范围?

我似乎无法在不将范围限定到基类的情况下调用基类的方法,这似乎是因为我重载了该方法。如果我不重载该方法,那么编译器不会抱怨。这是我想做的一个例子:

struct BaseClass {                                                           
  template <typename T> T& foo(T& t) {                               
    return t;                                                                
  }                                                                          
};                                                                           

class ChildClass: public BaseClass {                                         
  public:
  // The presence of this template causes compiler confusion                                                            
  template <class T> T& foo(T& t, int szl) {                        
    return t;                                                                
  }                                                                          

  template <class T> int bar(T& t) {                 
    // But if I scope this as BaseClass::foo(...) it's all good                        
    return foo(t);                                                
  }                                                                          
};                                                                           

int main() {                                                                 
  int t = 1;                                                                 
  ChildClass c;                                                              
  c.bar(t);                                        
}
Run Code Online (Sandbox Code Playgroud)

如果在 bar(...) 中我调用 BaseClass::foo(...) 编译器不会抱怨,但我在这里没有看到任何歧义,所以我很困惑为什么我需要这样做。

c++ templates

3
推荐指数
1
解决办法
259
查看次数

当存在滚动条时,在自举流体跨度中,Highcharts图形宽度不正确

我不确定这是一个bootstrap或highcharts问题,但我似乎无法使我的图表正确调整大小; 当存在滚动条和引导跨度时,初始图表宽度太宽.调整窗口大小似乎永远不会产生太宽的宽度,但有时它们太窄.

从span构造中删除代码似乎在所有情况下都会产生适当的宽度,因此我认为hc和bootstrap之间可能存在一些有害的交互.

我做了一个演示小提琴,但请记住,您的浏览器窗口需要足够短以导致滚动条:

http://jsfiddle.net/xEtherealx/Q5EGX/15/

有没有办法可以通过CSS强制宽度符合?我可以在窗口调整大小上创建一个回调作为一种解决方法,但我宁愿找到一个合适的解决方案.

编辑:根据建议,以下是一种解决方法,可以在一些超时后调用,或者在页面加载后调用一次:

    this.setChartSize = function() {
        chart.setSize( $(chart.container).parent().width(), $(chart.container).parent().height() );
        return false;
    };
Run Code Online (Sandbox Code Playgroud)

HTML:

<div id="content" class="container-fluid">
    <div class="row-fluid">
        <div id="toresize" class="span3" style="background: gray; overflow: auto;">
            <div class="targetpane">
                 <h4 class="text-center">HC Sizing Test</h4>

                <!-- take up space -->
                <div class="well well-small" style="height: 160px;"></div>

                <!-- Chart -->
                <div class="well well-small" style="padding: 5px;">
                    <div id="barchart" style="height: 160px; margin-bottom: 5px;"></div>
                </div>

                <!-- take up space -->
                <div class="well well-small" style="height: 160px;"></div>
            </div>
        </div>

        <!-- span --> …
Run Code Online (Sandbox Code Playgroud)

css highcharts twitter-bootstrap

1
推荐指数
1
解决办法
5464
查看次数