Skip to content

solve: 31.Next Permutation#58

Open
t9a-dev wants to merge 1 commit intomainfrom
31.Next-Permutation
Open

solve: 31.Next Permutation#58
t9a-dev wants to merge 1 commit intomainfrom
31.Next-Permutation

Conversation

@t9a-dev
Copy link
Copy Markdown
Owner

@t9a-dev t9a-dev commented Mar 13, 2026

問題: 31. Next Permutation
次に解く問題: 8. String to Integer (atoi)
ファイルの構成: ./src/bin/<各ステップ>.rs

Comment thread src/bin/step2.rs
capacityが0からスタートしたとき、100回insertするとメモリ領域の伸張は合計6回発生する。
0 + 3 + 7 + 14 + 28 + 56 = 108
108 / 10 ^ 8 = 0.00000108s = 1.08μs となる。
1.08μsを節約するためにHashSet::with_capacity(emails.len())を理解して書いているのであれば、ソフトウェアエンジニアの常識から外れている行為だなと思った。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

実際のところコピー自体はもう少し速いかもしれません。CPU のクロックと SIMD やメモリー帯域の関係で。ただ、この場合、安全側に倒すのに越したことはないです。
一方で、OS がメモリーのアロケーションをするので、API 呼び出しなどもろもろコストは乗るでしょう。

まあ、結論はあまり違和感がありませんが、結構適当な計算であるというふうに思っていただいたほうがいいかと思います。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

with_capacity 自体はつけてもいいと思いますが、大まかな見積もりは意識しておきましょう、ということです。

Comment thread src/bin/step3.rs
nums.reverse();
return;
};
let Some(swap_index) = nums
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

上を rfind_first_decreasing_index にするなら、こちらも next_larger_index など、内容を表す命名にしたいです。swapすることは後の処理で分かりますし。

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.

3 participants