python map 和 for 效率對比

import time
import numpy as np

y = list(np.arange(1, 77777))

def func(x):
    global y
    if x in y:
        result = True
    else:
        result = False
    return result

check_list = list(np.arange(1, 20000))

print('開始程序')

print('開始第一個程序')
# map
start_time1 = time.time()
A = list(map(func, check_list))
end_time1 = time.time()

# ==============================================================

print('開始第二個程序')
# for
start_time2 = time.time()
B = []
for check_str in check_list:
    if check_str in y:
        B.append(True)
    else:
        B.append(False)

end_time2 = time.time()

print('結束程序')

print("map cost: %f" % (end_time1 - start_time1))
print("for cost: %f" % (end_time2 - start_time2))

好像 for 比 map 速度快一點

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章