Skip to content

Conversation

@potrue
Copy link
Owner

@potrue potrue commented Aug 10, 2025

} else if (nums[mid] < nums[right]) {
right = mid;
} else { // if (nums[mid] == nums[right])
--right;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

すべての値が等しい場合、 O(n) になるように思いました。


## 最終コード

条件分岐って全部単体のifで回すよりelseとかcontinueとかつけておいた方が余計な条件分岐の判定が少なくなってちょっと早くなるとかあるんでしょうか?
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

もし気になるようであれば、アセンブラーを出力させて比較したり、マイクロベンチマークを取るとよいと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど。アセンブラーを出力するのは考えませんでした。
https://godbolt.org/
このサイトでやってみたところ、x86-64 clang 20.1.0というのでやった場合は-O2, -O3といった最適化オプションを付けた場合は変わらないようです 👀


条件分岐って全部単体のifで回すよりelseとかcontinueとかつけておいた方が余計な条件分岐の判定が少なくなってちょっと早くなるとかあるんでしょうか?
コンパイラが勝手に最適化してくれたりすることもあるんですかね。
もしかしたらこのelseは少し見づらいかも・・・

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

全体としては良いと思いました。if 文には {} ついている方が個人的には好みです。

Copy link

@ryosuketc ryosuketc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM です

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants