Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In general you just shouldn't worry about performance at this level. Very often the things that end up being performance problems turn out to be completely different from what you might expect, especially if you don't have a lot of experience with performance optimization.</p> <p>You should always think about writing clear code first, and if performance matters then you should think about it in algorithmic terms (i.e., big-O). Then you should measure performance and let that guide where you spend your effort at optimization.</p> <hr> <p>Now, you can make the code even clearer and more straightforward if you avoid the intermediate array and just use an array for the original objects:</p> <pre><code>MyObject obj[4]; for (int i = 0; i !=4; ++i) objs[i].doSomming(arg); </code></pre> <hr> <p>But no, an optimizing compiler should generally have no problem with this.</p> <p>For example, if I take the code:</p> <pre><code>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[] = {&amp;obj1, &amp;obj2, &amp;obj3, &amp;obj4}; for (int i = 0; i !=4; ++i) objs[i]-&gt;doSomming(); } void foo3() { MyObject obj[4]; for (int i = 0; i !=4; ++i) obj[i].doSomming(); } </code></pre> <p>and produce LLVM IR (because it's more compact than actual assembly), I get the following with <code>-O3</code>.</p> <pre><code>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 } </code></pre> <p>At <code>-O3</code> the loop gets unrolled and the code is identical with the original version. With <code>-Os</code> the loops don't get unrolled, but the the pointer indirection and even the arrays disappear because they're not needed after inlining:</p> <pre><code>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 } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload