Please help to optimize my python code, I wrote this JS code and tried to convert it to python that I need the execution time comparable to the original code.
The original code follows for-loop in JS, I transferred the code literally by using pure python and numpy but I didn't optimize it well resulting in poor execution time and memory usage.
This code calculates a preset enemy vs player power with +-10% difference and do a random roll then get the player winrate. The target is to set the HERO_POWER or ENEMY_POWER atleast in 100000-300000 range.
'''
##winchance.js##
Original code
var h = 0
var g = 0
var i = char_power
var l = i*.9
var u = enemy_power * 1.1
var d = enemy_power * .9
for (let e = Math.floor(l); e <= i; e++)
for (let t = Math.floor(d); t <= u; t++) e >= t ? h++ : g++
var z = h / (h + g)*100;
return z
}
var out = combat_simulate(parseInt(arguments[2]),parseInt(arguments[3]))
console.log(out)
'''
import subprocess,itertools,math
import numpy as np
def calc_iter(array):
w=0
l=0
for x,y in array:
if x >= y:
w +=1
else:
l +=1
return w,l
###inputs###
HERO_POWER=120000
ENEMY_POWER=110000
w=0
l=0
h_l = HERO_POWER * .9
e_h = ENEMY_POWER * 1.1
e_l = ENEMY_POWER * .9
hp = np.arange(math.floor(h_l),math.floor(HERO_POWER))
ep = np.arange(math.floor(e_l),math.floor(e_h))
print('process using itertools')
start_time = time.time()
array = itertools.product(hp,ep)
w,l = calc_iter(array)
print('win rate:{}%'.format(round((w/(w+l))*100,2)))
end_time = time.time()
print('time elapsed',end_time-start_time)
print()
print('process using numpy')
start_time = time.time()
x,y = np.meshgrid(hp,ep)
n = x>y
w,l = (np.count_nonzero(n),np.count_nonzero(n==0))
print('win rate:{}%'.format(round((w/(w+l))*100,2)))
end_time = time.time()
print('time elapsed',end_time-start_time)
print()
print('process using nodejs')
start_time = time.time()
result = subprocess.run('node winchance.js {} {}'.format(HERO_POWER,ENEMY_POWER),capture_output=True,text=True)
print('win rate:{}%'.format(round(float(result.stdout),2)))
end_time = time.time()
print('time elapsed',end_time-start_time)
########################################
process using itertools
win rate:68.18%
time elapsed 30.484147787094116
process using numpy
win rate:68.18%
time elapsed 2.0294463634490967
process using nodejs
win rate:68.18%
time elapsed 0.799668550491333