以下の2つのソースコード test1.c
と test2.c
はどちらのほうが速いか、コードを見て予想せよ。
ただし、条件は以下のようにする:
gcc -O2 test1.c -o test1
gcc -O2 test2.c -o test2
time ./test1
time ./test2
この問題はコンパイラの最適化に関する問題であるため、バージョンによっては作題意図通りの結果にならないかもしれない。作者は以下のバージョンで確認した。
$ gcc --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.