【问题标题】:Using temporary arrays to cut down on code - inefficient?使用临时数组来减少代码 - 效率低下?
【发布时间】:2012-11-17 04:18:42
【问题描述】:

我是 C++(和 SO)的新手,如果这很明显,我很抱歉。

我已经开始在我的代码中使用临时数组来减少重复并更容易对多个对象执行相同的操作。所以而不是:

MyObject obj1, obj2, obj3, obj4;

obj1.doSomming(arg);
obj2.doSomming(arg);
obj3.doSomming(arg);
obj4.doSomming(arg);

我在做:

MyObject obj1, obj2, obj3, obj4;
MyObject* objs[] = {&obj1, &obj2, &obj3, &obj4};

for (int i = 0; i !=4; ++i)
    objs[i]->doSomming(arg);

这对性能有害吗?比如,它会导致不必要的内存分配吗?这是好习惯吗?谢谢。

【问题讨论】:

  • std::array<MyObject, 4> objs; for (const MyObject &obj : objs) obj.doSomming(arg);
  • 如果有差异,它可能不会被注意到。如果有的话,我认为第一个实际上更干净。
  • 数组很好,但即使没有容器,您也不需要指针数组。将第二个示例更改为 MyObject objs[4]; 无需额外丑陋的指针语法即可工作,但原始数组也可以替换为容器。
  • 如果您使用 -funroll-loops 进行优化,它将看起来与您的第一个示例完全一样,只是使用额外的堆栈变量来保存指针,这可能会也可能不会被优化。
  • 是否有任何理由需要命名各个对象?为什么不直接使用MyObject objs[4]; for (int i = 0; i != 4; ++i) objs[i].doSomming(arg); }

标签: c++ arrays performance


【解决方案1】:

一般而言,您不必担心此级别的性能。很多时候,最终成为性能问题的事情与您的预期完全不同,尤其是在您没有很多性能优化经验的情况下。

您应该始终首先考虑编写清晰的代码,如果性能很重要,那么您应该从算法方面考虑它(即 big-O)。然后,您应该衡量性能并让其指导您在优化方面的努力。


现在,如果您避免使用中间数组而只对原始对象使用数组,则可以使代码更加清晰和直接:

MyObject obj[4];

for (int i = 0; i !=4; ++i)
  objs[i].doSomming(arg);

但是不,优化编译器通常应该对此没有问题。

例如,如果我拿代码:

struct MyObject {
    void doSomming() {
        std::printf("Hello\n");
    }
};

void foo1() {
    MyObject obj1, obj2, obj3, obj4;

    obj1.doSomming();
    obj2.doSomming();
    obj3.doSomming();
    obj4.doSomming();
}

void foo2() {
    MyObject obj1, obj2, obj3, obj4;
    MyObject* objs[] = {&obj1, &obj2, &obj3, &obj4};

    for (int i = 0; i !=4; ++i)
        objs[i]->doSomming();
}

void foo3() {
    MyObject obj[4];

    for (int i = 0; i !=4; ++i)
        obj[i].doSomming();
}

并生成 LLVM IR(因为它比实际组装更紧凑),我得到以下 -O3

define void @_Z4foo1v() nounwind uwtable ssp {
entry:
  %puts.i = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
  %puts.i1 = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
  %puts.i2 = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
  %puts.i3 = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
  ret void
}

define void @_Z4foo2v() nounwind uwtable ssp {
entry:
  %puts.i = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
  %puts.i.1 = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
  %puts.i.2 = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
  %puts.i.3 = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
  ret void
}

define void @_Z4foo3v() nounwind uwtable ssp {
entry:
  %puts.i = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
  %puts.i.1 = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
  %puts.i.2 = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
  %puts.i.3 = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
  ret void
}

-O3 处,循环展开并且代码与原始版本相同。使用-Os,循环不会展开,但指针间接甚至数组都会消失,因为在内联后不需要它们:

define void @_Z4foo2v() nounwind uwtable optsize ssp {
entry:
  br label %for.body

for.body:                                         ; preds = %entry, %for.body
  %i.05 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
  %puts.i = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
  %inc = add nsw i32 %i.05, 1
  %cmp = icmp eq i32 %inc, 4
  br i1 %cmp, label %for.end, label %for.body

for.end:                                          ; preds = %for.body
  ret void
}

define void @_Z4foo3v() nounwind uwtable optsize ssp {
entry:
  br label %for.body

for.body:                                         ; preds = %entry, %for.body
  %i.03 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
  %puts.i = tail call i32 @puts(i8* getelementptr inbounds ([6 x i8]* @str, i64 0, i64 0)) nounwind
  %inc = add nsw i32 %i.03, 1
  %cmp = icmp eq i32 %inc, 4
  br i1 %cmp, label %for.end, label %for.body

for.end:                                          ; preds = %for.body
  ret void
}

【讨论】:

  • 这是一个优秀的答案,并提出了一个非常重要的观点:尽量不要担心性能;只需以有意义的方式编写干净且可维护的代码。然后如果有证据表明存在性能问题,请返回并解决它。 唯一的例外是,如果您拥有多年的优化经验,可以让您及早洞察潜在的瓶颈和避免它们的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-13
  • 1970-01-01
  • 2014-05-04
  • 2021-03-24
  • 2017-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多