0
0

More than 1 year has passed since last update.

Python: itertools.repeat と range によるループの実行時間の比較

Last updated at Posted at 2021-12-26

Python: itertools.repeat と range によるループの実行時間の比較

はじめに

range()によるループは、作り出された値を使わない場合は無駄なんじゃないかと思いもっと良い方法がないか調べてみました。
そうすると、itertools.repeat()が良いとの記述を見つけました。
itertools.repeat(None,n)は、Noneをn回返す関数ですが、実際の値ではなくて参照を返すので速いようです。
では、どの程度実行時間に差があるのか調べてみました。

結論

値が要らないループならば、itertools.repeat(None,n)の方が速い。

環境

OS
エディション  Windows 11 Pro
バージョン 21H2
OS ビルド    22000.318
エクスペリエンス    Windows 機能エクスペリエンス パック 1000.22000.318.0
システムの種類   64 ビット オペレーティング システム、x64 ベース プロセッサ
python
Python 3.10.1 (tags/v3.10.1:2cd268a, Dec  6 2021, 19:10:37) [MSC v.1929 64 bit (AMD64)]

コード

python
import gc
import time
import timeit
import itertools
import functools
import statistics

l = 10000
n = 10000
r = 100


def funcRange():
  result = 0
  for _ in range(l):
    result += 1


def funcRepeat():
  result = 0
  for _ in itertools.repeat(None, l):
    result += 1


def printStatistics(lt):
  fmt = ".10f"
  print(f"{lt}")
  print(f"合計      : {sum(lt):{fmt}}")
  print(f"平均      : {statistics.mean(lt):{fmt}}")
  print(f"中央値    : {statistics.median(lt):{fmt}}")
  print(f"最頻値    : {statistics.mode(lt):{fmt}}")
  print(f"母標準偏差: {statistics.pstdev(lt):{fmt}}")
  print(f"母分散    : {statistics.pvariance(lt):{fmt}}")


gc.disable()
t1 = timeit.repeat(stmt=funcRange, number=n, repeat=r)
t2 = timeit.repeat(stmt=funcRepeat, number=n, repeat=r)
gc.enable()

printStatistics(t1)
printStatistics(t2)

結果

環境による影響を受けますので参考程度に見ていただきたいですが、目に見える差があるようです。

実行結果
[3.0125306000118144, 3.0121964999998454, 3.012961999993422, 3.011133799998788, 3.0034582000080263, 3.0043995999876643, 3.0015363999991678, 3.0115586000028998, 3.016521899990039, 3.015952500005369, 3.0168217000027653, 3.0116928000061307, 3.046082800006843, 3.016702900000382, 3.02881289999641, 3.018427899995004, 3.0084847999969497, 3.007094999993569, 3.0051706999947783, 3.019679899996845, 3.002158600007533, 3.006728899999871, 3.003721700006281, 3.019018099992536, 3.025198300005286, 3.0073662999930093, 3.029435000004014, 3.040383899991866, 3.058797100005904, 3.0478392999939388, 3.0170709999947576, 3.031825399986701, 3.0338826999941375, 3.0416884000005666, 3.062740399996983, 3.057281299988972, 3.0389201999932993, 3.0510797999886563, 3.04364849999547, 3.046230000007199, 3.0308374000014737, 3.0428332000010414, 3.0339428999868687, 3.0042433999915374, 3.0005132000078447, 3.0162721000087913, 3.009457400010433, 3.005504899992957, 3.0315924999886192, 3.021166799997445, 3.0408429999952205, 3.0289544999977807, 3.0339224000053946, 3.011512600001879, 3.0556810999987647, 3.028512100005173, 3.040680100006284, 3.055385899991961, 3.0358662999933586, 3.0493245999969076, 3.052616000000853, 3.0363882999954512, 3.0162394999933895, 3.0125674000009894, 3.020197099991492, 3.019747300000745, 3.023779099996318, 3.018473999996786, 3.036460100003751, 3.011348200001521, 3.002166099991882, 3.01119170000311, 3.011751500001992, 3.0650847000069916, 3.038250600002357, 3.0076160000025993, 3.0198543000005884, 3.0175554000015836, 3.123675299997558, 3.0382258000026923, 3.031871899991529, 3.042203899996821, 3.0309672000003047, 3.044741299992893, 3.0441513000114355, 3.0526781999942614, 3.0344398000015644, 3.046831100000418, 3.0594153000129154, 3.0410628999961773, 3.0475103999924613, 3.0879609000112396, 3.0513352999987546, 3.0844190000061644, 3.0334161999926437, 3.0339449000020977, 3.0279550999985076, 3.03104680000979, 3.047398199996678, 3.038842299996759]
合計      : 303.0186631999
平均      : 3.0301866320
中央値    : 3.0301362000
最頻値    : 3.0125306000
母標準偏差: 0.0208148344
母分散    : 0.0004332573
[2.471044300007634, 2.4559371000068495, 2.461155000011786, 2.5613831000082428, 2.602027100001578, 2.5422525000030873, 2.519182300005923, 2.469540700010839, 2.4628415999904973, 2.464828399999533, 2.457431100003305, 2.460813599987887, 2.5137097999977414, 2.5082216000009794, 2.6013787999982014, 2.5880179999949178, 2.499785600011819, 2.511878599994816, 2.4695549000025494, 2.5938626999995904, 2.5935980999929598, 2.4704123999981675, 2.4758150999987265, 2.4791915000096196, 2.484101500012912, 2.473916699993424, 2.4500641000049654, 2.4970853999984683, 2.5090997999941465, 2.5017172999941977, 2.472311900011846, 2.483352700000978, 2.472138500001165, 2.4734089999983553, 2.4567908000026364, 2.4775198999996064, 2.4687792000040645, 2.4673344000038924, 2.486771800002316, 2.4933445999922697, 2.511952199987718, 2.4816195000021253, 2.4554998999956297, 2.4553866000060225, 2.4520639999973355, 2.458470200013835, 2.4515162999887252, 2.455106599998544, 2.477517600011197, 2.474436100004823, 2.471783699991647, 2.449305499991169, 2.478230499997153, 2.4675087000068743, 2.4743155000032857, 2.464601200001198, 2.4678409000043757, 2.4710044999956153, 2.4622966999886557, 2.4602322000137065, 2.4561036999948556, 2.4595081000006758, 2.4595932999945944, 2.4912881000054767, 2.5129182999953628, 2.528319200006081, 2.490331300010439, 2.520631500010495, 2.4765188000019407, 2.825662999995984, 2.9455691000039224, 2.7070437999936985, 2.552530499990098, 2.485951799986651, 2.4561227000085637, 2.4438591999933124, 2.45256989999325, 2.4394605999987107, 2.434516799999983, 2.4446970999997575, 2.467963700008113, 2.5332236000103876, 2.5133110000024317, 2.5089592000003904, 2.6053364999970654, 2.535974200000055, 2.493532699998468, 2.4784337999881245, 2.489058599996497, 2.4952079999929992, 2.458859800011851, 2.4814831000112463, 2.48858969999128, 2.4887877000001026, 2.479933099995833, 2.4797443999996176, 2.48026349999418, 2.4820003999921028, 2.4724636000028113, 2.474555799999507]
合計      : 249.7331691000
平均      : 2.4973316910
中央値    : 2.4775187500
最頻値    : 2.4710443000
母標準偏差: 0.0706183769
母分散    : 0.0049869552
0
0
0

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
0
0