フィボナッチ数 -->
> > This page is based on a Wikipedia article written by contributors (read/edit).
Text フィボナッチ数列とは is available under the CC BY-SA 4.0 license; additional terms may apply.
Images, videos and audio are available under their respective licenses.
Tell your friends about Wikiwand!
Suggest as cover photo
Would you like to suggest this photo as the cover photo for this article?
Thank you for helping!
Your input will affect cover photo selection, along with input from other users.
Thanks for reporting this video!
This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of フィボナッチ数列とは the following browsers:
An extension you use may be preventing Wikiwand articles from フィボナッチ数列とは loading properly.
If you'フィボナッチ数列とは re using HTTPS Everywhere or you're unable to access any article on Wikiwand, please consider switching to HTTPS (https://www.wikiwand.com).
An extension you use may be preventing Wikiwand articles from loading properly.
If you are using an Ad-Blocker, it might have mistakenly blocked our content. You will need to temporarily disable your Ad-blocker to フィボナッチ数列とは view this page.
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
フィボナッチ数列とは
再帰使わない
Golangのsliceを使って実装しています。こうすれば再帰は使わずに実装できますね
ではベンチマーク行ってみましょう
ループの回数を減らしてみる
今までは i 分だけ回してましたが、 i/2 分だけにしてみましょう
だんだん読みづらくなってきました…
ベンチマーク行ってみましょう
17.4ns!!どんどん早くなって行ってますね
ここまでは自分で工夫して行きましたがそろそろ限界なのでいろいろ調べてみましょう
Golangのパフォーマンスチューニング
- メモリのアロケーション回数を減らす
- 要素数が事前にわかっている場合には append を使わない
- channel を使わない
- 関数(メソッド)を呼ばない
などなど…
いろいろ調べましたが、今回のコードで悪いところといえばsliceを使っているところですかね?(本来はトリニボッチとかに対応するためにsliceにしたのですが…)
ではこれをただのIntの変数に変えてみましょう
これが一番早いと思います
見る限り爆速そうですね(適当)
ではベンチマークしてみましょう
え?5.20ns?めちゃくちゃ早くなった!!!!
やっぱり毎回sliceでアクセスするのは遅いですね(というかsliceの値にアクセスするのに%演算子要らなくないか…?)
コメント