Skip to content

Instantly share code, notes, and snippets.

@desg
Last active August 29, 2015 13:57
Show Gist options
  • Save desg/9858154 to your computer and use it in GitHub Desktop.
Save desg/9858154 to your computer and use it in GitHub Desktop.
how can I optimize 100k rounds 10-15 rolls per round
import random
from math import *
class player:
victories = 0
def set_levels(self, attack, strength, defense):
self.attack_level = attack
self.strength_level = strength
self.defense_level = defense
def set_opponent_levels(self, attack, strength, defense):
self.opponent_attack_level = attack
self.opponent_strength_level = strength
self.opponent_defense_level = defense
def set_bonuses(self, attack, strength, defense):
self.attack_bonus = attack
self.strength_bonus = strength
self.defense_bonus = defense
def set_multiplier(self, mult):
self.multiplier = mult
def set_name(self, playername):
self.name = playername
def set_style(self, attackstyle):
if attackstyle == "a":
self.attack_style = 3
self.strength_style = 0
self.defense_style = 0
elif attackstyle == "s":
self.attack_style = 0
self.strength_style = 3
self.defense_style = 0
elif attackstyle == "d":
self.attack_style = 0
self.strength_style = 0
self.defense_style = 3
elif attackstyle == "c":
self.attack_style = 1
self.strength_style = 1
self.defense_style = 1
else:
return 1
def set_hitpoints(self, hp):
self.hitpoints = hp
self.remaining_hitpoints = hp
def max_rolls(self):
self.ea = self.attack_level + self.attack_style + 8
self.mar = self.ea * (1 + self.attack_bonus/64)
self.max_attack_roll = self.mar
self.es = self.strength_level + self.strength_style + 8
self.msr = self.es * (1 + self.strength_bonus/64)
self.max_strength_roll = self.msr
self.ed = self.defense_level + self.defense_style + 8
self.mdr = self.ed * (1 + self.defense_bonus/64)
self.max_defense_roll = self.mdr
def roll(self):
self.attack_roll = floor(random.randint(0, self.max_attack_roll) / 10)
self.strength_roll = floor(random.randint(0, self.max_strength_roll) / 10)
self.defense_roll = floor(random.randint(0, self.max_defense_roll) / 10)
def attack(self, defense):
if self.attack_roll > defense:
self.damage = self.strength_roll
else:
self.damage = 0
player1 = player()
player2 = player()
#swag = combatlookup()
iterations = 100000
player1.set_name("badgerdick")
#swag.get_rsn(player1.name)
player1.set_levels(99, 99, 99)
player1.set_bonuses(82,82,0)
player1.set_style("c")
player1.set_hitpoints(88)
player2.set_name("wrong deer")
#swag.get_rsn(player2.name)
player2.set_levels(95, 90, 99)
player2.set_bonuses(82,82,0)
player2.set_style("c")
player2.set_hitpoints(99)
player1.set_opponent_levels(player2.attack_level, player2.strength_level, player2.defense_level)
player2.set_opponent_levels(player1.attack_level, player1.strength_level, player2.defense_level)
player1.max_rolls()
player2.max_rolls()
dead = False
for i in range(iterations/2):
while not dead:
player1.roll()
player2.roll()
player1.attack(player2.defense_roll)
player2.remaining_hitpoints -= player1.damage
if player2.remaining_hitpoints <= 0:
dead = True
player1.victories += 1
else:
player2.attack(player1.defense_roll)
player1.remaining_hitpoints -= player2.damage
if player1.remaining_hitpoints <= 0:
dead = True
player2.victories += 1
player1.remaining_hitpoints = player1.hitpoints
player2.remaining_hitpoints = player2.hitpoints
dead = False
for i in range(iterations/2):
while not dead:
player1.roll()
player2.roll()
player2.attack(player1.defense_roll)
player1.remaining_hitpoints -= player2.damage
if player1.remaining_hitpoints <= 0:
dead = True
player2.victories += 1
else:
player1.attack(player2.defense_roll)
player2.remaining_hitpoints -= player1.damage
if player2.remaining_hitpoints <= 0:
dead = True
player1.victories += 1
player1.remaining_hitpoints = player1.hitpoints
player2.remaining_hitpoints = player2.hitpoints
dead = False
print "%s : %d" % (player1.name, player1.victories)
print "%s : %d" % (player2.name, player2.victories)
@saml
Copy link

saml commented Apr 1, 2014

desg : 55911
elve : 44089
         31300739 function calls in 42.007 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.004    0.004   42.007   42.007 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 __future__.py:48(<module>)
        1    0.000    0.000    0.000    0.000 __future__.py:74(_Feature)
        7    0.000    0.000    0.000    0.000 __future__.py:75(__init__)
        1    4.967    4.967   42.002   42.002 gistfile1.py:1(<module>)
        2    0.000    0.000    0.000    0.000 gistfile1.py:13(set_opponent_levels)
        2    0.000    0.000    0.000    0.000 gistfile1.py:19(set_bonuses)
        2    0.000    0.000    0.000    0.000 gistfile1.py:29(set_name)
        2    0.000    0.000    0.000    0.000 gistfile1.py:33(set_style)
        1    0.000    0.000    0.000    0.000 gistfile1.py:4(player)
        2    0.000    0.000    0.000    0.000 gistfile1.py:55(set_hitpoints)
        2    0.000    0.000    0.000    0.000 gistfile1.py:60(max_rolls)
        2    0.000    0.000    0.000    0.000 gistfile1.py:7(set_levels)
  2239756    8.522    0.000   35.727    0.000 gistfile1.py:75(roll)
  2183845    1.302    0.000    1.302    0.000 gistfile1.py:80(attack)
        1    0.001    0.001    0.001    0.001 hashlib.py:55(<module>)
        6    0.000    0.000    0.000    0.000 hashlib.py:94(__get_openssl_constructor)
        1    0.000    0.000    0.000    0.000 random.py:100(seed)
  6719268   18.019    0.000   19.446    0.000 random.py:173(randrange)
  6719268    6.019    0.000   25.465    0.000 random.py:236(randint)
        1    0.001    0.001    0.002    0.002 random.py:40(<module>)
        1    0.000    0.000    0.000    0.000 random.py:650(WichmannHill)
        1    0.000    0.000    0.000    0.000 random.py:72(Random)
        1    0.000    0.000    0.000    0.000 random.py:800(SystemRandom)
        1    0.000    0.000    0.000    0.000 random.py:91(__init__)
        1    0.000    0.000    0.000    0.000 {_hashlib.openssl_md5}
        1    0.000    0.000    0.000    0.000 {_hashlib.openssl_sha1}
        1    0.000    0.000    0.000    0.000 {_hashlib.openssl_sha224}
        1    0.000    0.000    0.000    0.000 {_hashlib.openssl_sha256}
        1    0.000    0.000    0.000    0.000 {_hashlib.openssl_sha384}
        1    0.000    0.000    0.000    0.000 {_hashlib.openssl_sha512}
        1    0.000    0.000    0.000    0.000 {binascii.hexlify}
        1    0.000    0.000    0.000    0.000 {function seed at 0x7fd13d0441b8}
        6    0.000    0.000    0.000    0.000 {getattr}
        6    0.000    0.000    0.000    0.000 {globals}
        1    0.000    0.000    0.000    0.000 {math.exp}
  6719268    1.740    0.000    1.740    0.000 {math.floor}
        2    0.000    0.000    0.000    0.000 {math.log}
        1    0.000    0.000    0.000    0.000 {math.sqrt}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
  6719268    1.427    0.000    1.427    0.000 {method 'random' of '_random.Random' objects}
        1    0.000    0.000    0.000    0.000 {posix.urandom}
        1    0.003    0.003    0.003    0.003 {range}

@saml
Copy link

saml commented Apr 1, 2014

    def roll(self):
        self.attack_roll    = floor(randint(0, self.max_attack_roll) / 10)
        self.strength_roll  = floor(randint(0, self.max_strength_roll) / 10)
        self.defense_roll   = floor(randint(0, self.max_defense_roll) / 10)

    def attack(self, defense):
        if self.attack_roll > defense:
            self.damage = self.strength_roll
        else:
            self.damage = 0

def randint(a,b):
    return a+random.random()*(b-a)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment