Created
October 26, 2018 18:47
-
-
Save YSRKEN/065e0ecce3928457c9609ce4da349c0f to your computer and use it in GitHub Desktop.
艦これの計算を高速化するTipsまとめ その1 ref: https://qiita.com/YSRKEN/items/bd51a46e0be6e65888c5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
○対象となる艦載機の一覧を取得 | |
[零観, 紫雲, 夜偵, 天山村田, 九七友永, 九七友永, 天山友永, 天山村田, 流星改] | |
○命中値で安定ソート | |
[零観(命中2), 紫雲(1), 夜偵(1), 天山村田(2), 九七友永(3), 九七友永(3), 天山友永(3), 天山村田(2), 流星改(0)] | |
↓ | |
[九七友永(3), 九七友永(3), 天山友永(3), 零観(2), 天山村田(2), 天山村田(2), 紫雲(1), 夜偵(1), 流星改(0)] | |
○それぞれの触接選択率を計算 | |
[九七友永(索敵4), 九七友永(4), 天山友永(5), 零観(6), 天山村田(4), 天山村田(4), 紫雲(8), 夜偵(3), 流星改(2)] | |
↓ | |
[九七友永(28%), 九七友永(28%), 天山友永(35%), 零観(42%), 天山村田(28%), 天山村田(28%), 紫雲(56%), 夜偵(21%), 流星改(14%)] | |
○実際に選択される確率を求める。先頭から順に判定されることに注意 | |
[九七友永(28%), 九七友永(20.2%), 天山友永(18.1%), 零観(14.2%), 天山村田(5.5%), 天山村田(3.9%), 紫雲(5.7%), 夜偵(0.9%), 流星改(0.5%)] | |
※どれも選択されず、触接に失敗する確率は3.0% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
制空状態の判定(簡単な書き方) | |
経過時間:5.383422374725342[s] | |
処理時間:0.5383422374725342[us/回] | |
制空状態の判定(高速な書き方) | |
経過時間:2.907625675201416[s] | |
処理時間:0.29076256752014157[us/回] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
St1撃墜処理(簡単な書き方) | |
経過時間:2.6160950660705566[s] | |
処理時間:2.616095066070556[us/回] | |
St1撃墜処理(高速な書き方) | |
経過時間:1.1893696784973145[s] | |
処理時間:1.1893696784973145[us/回] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from time import time | |
def get_fighter_combat_result(fighter_power: int, enemy_fighter_power: int) -> int: | |
"""制空状態の判定(通常版) | |
""" | |
if fighter_power >= 3 * enemy_fighter_power: | |
return 0 # 制空権確保 | |
elif fighter_power * 2 >= 3 * enemy_fighter_power: | |
return 1 # 航空優勢 | |
elif fighter_power * 3 > 2 * enemy_fighter_power: | |
return 2 # 航空均衡 | |
elif fighter_power * 3 > enemy_fighter_power: | |
return 3 # 航空劣勢 | |
else: | |
return 4 # 制空権喪失 | |
# 事前計算する | |
max_fighter_power: int = 1000 # 最大の制空値 | |
result_list = [[0 for i in range(max_fighter_power)] for j in range(max_fighter_power)] # キャッシュ | |
for i in range(0, max_fighter_power): | |
for j in range(0, max_fighter_power): | |
if i >= 3 * j: | |
result_list[i][j] = 0 # 制空権確保 | |
elif i * 2 >= 3 * j: | |
result_list[i][j] = 1 # 航空優勢 | |
elif i * 3 > 2 * j: | |
result_list[i][j] = 2 # 航空均衡 | |
elif i * 3 > j: | |
result_list[i][j] = 3 # 航空劣勢 | |
else: | |
result_list[i][j] = 4 # 制空権喪失 | |
def get_fighter_combat_result_fast(fighter_power: int, enemy_fighter_power: int) -> int: | |
"""制空状態の判定(高速) | |
""" | |
return result_list[fighter_power][enemy_fighter_power] | |
print('制空状態の判定(簡単な書き方)') | |
fighter_power: int = 120 # 艦隊の制空値 | |
enemy_fighter_power: int = 120 # 敵艦隊の制空値 | |
loop_size: int = 10000000 # ループサイズ | |
# 測定ループ開始 | |
start_time = time() | |
for _ in range(0, loop_size): | |
result = get_fighter_combat_result(fighter_power, enemy_fighter_power) | |
elapsed_time = time() - start_time | |
# 測定ループ終了 | |
print(f'経過時間:{elapsed_time}[s]') | |
print(f'処理時間:{elapsed_time / loop_size * 1000 * 1000}[us/回]') | |
print('制空状態の判定(高速な書き方)') | |
fighter_power: int = 120 # 艦隊の制空値 | |
enemy_fighter_power: int = 120 # 敵艦隊の制空値 | |
loop_size: int = 10000000 # ループサイズ | |
# 測定ループ開始 | |
start_time = time() | |
for _ in range(0, loop_size): | |
result = get_fighter_combat_result_fast(fighter_power, enemy_fighter_power) | |
elapsed_time = time() - start_time | |
# 測定ループ終了 | |
print(f'経過時間:{elapsed_time}[s]') | |
print(f'処理時間:{elapsed_time / loop_size * 1000 * 1000}[us/回]') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from time import time | |
import random | |
import math | |
def calc_new_friend_slots(fighter_combat_result: int, slot: int) -> int: | |
"""St1撃墜処理(通常版) | |
""" | |
if fighter_combat_result == 0: | |
# 制空権確保 | |
return slot - math.floor(slot * random.randint(7, 15) / 256) | |
elif fighter_combat_result == 1: | |
# 航空優勢 | |
return slot - math.floor(slot * random.randint(20, 45) / 256) | |
elif fighter_combat_result == 2: | |
# 制空均衡 | |
return slot - math.floor(slot * random.randint(30, 75) / 256) | |
elif fighter_combat_result == 3: | |
# 航空劣勢 | |
return slot - math.floor(slot * random.randint(40, 105) / 256) | |
else: | |
# 制空権喪失 | |
return slot - math.floor(slot * random.randint(65, 150) / 256) | |
# 事前計算する | |
max_slot: int = 100 # 最大の搭載数 | |
result_list = [[0 for i in range(max_slot)] for j in range(5)] | |
min_per = [7, 20, 30, 40, 65] | |
max_per = [15, 45, 75, 105, 150] | |
for i in range(0, 5): | |
for j in range(0, max_slot): | |
temp = list(map(lambda x: j - math.floor(j * x / 256), range(min_per[i], max_per[i] + 1))) | |
result_list[i][j] = temp | |
def calc_new_friend_slots_fast(fighter_combat_result: int, slot: int) -> int: | |
"""St1撃墜処理(高速) | |
""" | |
return random.choice(result_list[fighter_combat_result][slot]) | |
print('St1撃墜処理(簡単な書き方)') | |
fighter_combat_result: int = 2 # 制空状態 | |
friend_slot: int = 46 # 自艦隊のあるスロットの現搭載数 | |
loop_size: int = 1000000 # ループサイズ | |
# 測定ループ開始 | |
start_time = time() | |
for _ in range(0, loop_size): | |
result = calc_new_friend_slots(fighter_combat_result, friend_slot) | |
elapsed_time = time() - start_time | |
# 測定ループ終了 | |
print(f'経過時間:{elapsed_time}[s]') | |
print(f'処理時間:{elapsed_time / loop_size * 1000 * 1000}[us/回]') | |
print('St1撃墜処理(高速な書き方)') | |
fighter_combat_result: int = 2 # 制空状態 | |
friend_slot: int = 46 # 自艦隊のあるスロットの現搭載数 | |
loop_size: int = 1000000 # ループサイズ | |
# 測定ループ開始 | |
start_time = time() | |
for _ in range(0, loop_size): | |
result = calc_new_friend_slots_fast(fighter_combat_result, friend_slot) | |
elapsed_time = time() - start_time | |
# 測定ループ終了 | |
print(f'経過時間:{elapsed_time}[s]') | |
print(f'処理時間:{elapsed_time / loop_size * 1000 * 1000}[us/回]') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment