0
1

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 1 year has passed since last update.

Python 処理速度向上 Tips

Last updated at Posted at 2020-04-28

実行環境

MacBook Pro
python3.7.5

ループ

while i < N vs for _ in range(N)

i = 0
while i < N:  # N=10**6
    i += 1
for _ in range(N):
    pass

loop.png

1次元配列

配列の初期化

[None for _ in range(N)] vs [None] * N vs np.empty(N)

tmp = [None for _ in range(N)]
tmp = [None] * N
tmp = np.empty(N)

initilize_1dim_arr.png

配列の要素が全て数値で良いなら, Numpyを用いた方が速い

要素の変換

内包表記 vs map関数

tmp = [int(i) for i in range(N)]
tmp = list(map(int,range(N)))

comprihension_map.png

2次元配列(N×N)

[None for_in range(N)]for_in range(N)] vs [[None]*N for_in range(N)] vs np.empty((N,N))

tmp = [None for _ in range(N]for _ in range(N)]
tmp = [[None] * N for _ in range(N)]
tmp = np.empty((N,N))

initialize_2dim_arr.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?