-2

this question is derived from that post.

it seems that the online compiler discard this function while optimizing.

void functiona()
{
    long long number = 0;
    // long long problemSize = 100000000000;
    long long problemSize = 10;

    for( long long i = 0; i < problemSize; ++i )
    {
       for(long long j = 0; j < problemSize; j++)
       {
           for(long long k = 0; k < problemSize; k++)
           {
               for(long long l = 0; l < problemSize; l++)
               {
                   for(long long l = 0; l < problemSize; l++)
                   {
                       number++;
                       number--;
                   }
               }
            }
        }
    }
}

compiling this piece of code on my local mac

clang++ -std=c++11 -stdlib=libc++ benchmark.cpp

./a.out
409

seems to be without optimization.

this post and doc provide some info about clang optimization.

is there a way to enable optimization for clang++, so that the local output is close to online one.

  • Do you mean [`-O3`](https://godbolt.org/z/VDKA37)? – Guillaume Racicot Jun 12 '19 at 00:28
  • Frankly I'm amazed that compiles on your mac. using the posted source and verbatim build command on my mac with Apple LLVM version 10.0.1 (clang-1001.0.46.4) causes an ambiguity error on `function`. [Friends don't let friends use `using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – WhozCraig Jun 12 '19 at 00:43
  • 1
    Did you encounter some problem when reading the manual for your compiler? – Lightness Races in Orbit Jun 12 '19 at 00:50

1 Answers1

1

clang++ takes basically the same switches as clang, which are fully covered in the links you found on your own. The obvious one for maximum optimization would be -Ofast, but given how simple and obvious this optimization is, basically any level of optimization is likely to trigger it, -O3, -02, -Os, even -O1. The exact behavior will vary by compiler version, so you can experiment to determine the minimal level needed to eliminate the loop on your version.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271