1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

or列挙、リスト、タプル、集合でのif文の速度評価

Last updated at Posted at 2018-10-11

まえがき

Pythonにおけるif文の長いor列挙の回避方法

上記の記事で、if文においてorを列挙するよりリストのin演算子を使用したほうが少しスマートと書いた。
はたして速度面ではどうか。

計測

とても長いプログラムをベタ書きしたら記事にアクセスできなくなったので、プログラムは以下を参照してください。

結論

  • リストとタプルを使ったin演算子によるifの比較が最も速い(両方とも同程度の速さ)
  • orの列挙もリストと比べて遅すぎるというわけではない
  • 集合(set)は遅すぎる

メモリ使用量は考慮していない。
上記結果をふまえて、個人的にはやはりin演算子を使ったほうが少しスマートだと思う。

1
2
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?