jmcrosa / Anarchy_TPP_Ecruteak

Anarchy Simulator for Twitch Plays Pokemon: Ecruteak Gym
0 stars 1 forks source link

Speed improvements #3

Open rspilk opened 10 years ago

rspilk commented 10 years ago

Using the cProfile module, we can see where a majority of the time is spent during the program execution. If one command takes way more than anything else, improving the time it takes to do that command will drastically increase the simulation speed.

I ran the program for 84.16s and here is the output I got:

[tyler@archy ~/git/personal/TPP-sim]$  python -m cProfile twitchplays.py 
('-------SIM #', 1, 'TRY:', 1, '-------- Furthest is ', 0)
('-------SIM #', 1, 'TRY:', 980482, '-------- Furthest is ', 15)
^C         55203988 function calls in 84.160 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        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    0.000    0.000    0.000    0.000 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)
 13151163   29.703    0.000   33.302    0.000 random.py:173(randrange)
 13151163   11.610    0.000   44.912    0.000 random.py:236(randint)
        1    0.001    0.001    0.001    0.001 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   18.250   18.250   84.160   84.160 twitchplays.py:3(<module>)
 13151163   20.164    0.000   65.076    0.000 twitchplays.py:34(getMove)
        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 0x931fc34}
        6    0.000    0.000    0.000    0.000 {getattr}
        6    0.000    0.000    0.000    0.000 {globals}
  1225958    0.360    0.000    0.360    0.000 {len}
        1    0.000    0.000    0.000    0.000 {math.exp}
        2    0.000    0.000    0.000    0.000 {math.log}
        1    0.000    0.000    0.000    0.000 {math.sqrt}
  1373072    0.470    0.000    0.470    0.000 {method 'append' of 'list' objects}
       18    0.002    0.000    0.002    0.000 {method 'close' of 'file' objects}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
 13151163    3.598    0.000    3.598    0.000 {method 'random' of '_random.Random' objects}
      203    0.000    0.000    0.000    0.000 {method 'write' of 'file' objects}
       18    0.000    0.000    0.000    0.000 {open}
        1    0.000    0.000    0.000    0.000 {posix.urandom}
        1    0.000    0.000    0.000    0.000 {range}
       17    0.000    0.000    0.000    0.000 {time.asctime}

it shows that 13151163 29.703 0.000 33.302 0.000 random.py:173(randrange) 13151163 11.610 0.000 44.912 0.000 random.py:236(randint) 13151163 20.164 0.000 65.076 0.000 twitchplays.py:34(getMove)

are your big time hogs. Since randint is used for getMove, it makes sense. Your getMove is what is holding back the massive speed. Since this is all using fixed odds in a big if statement, it should be possible to optimize this through restructuring or even forcing a c-optimized switch statement. I will see what I can do.

rspilk commented 10 years ago

I was able to improve the speed about 3x without any changes except for using numpy's randint function instead of random

change import random

to from numpy import random

make sure numpy is installed.

jmcrosa commented 10 years ago

I was told using random.choice() also helps performance, but haven't tested.

On Wednesday, March 5, 2014, tspilk notifications@github.com wrote:

I was able to improve the speed about 3x without any changes except for using numpy's randint function instead of random

change import random

to from numpy import random

make sure numpy is installed.

Reply to this email directly or view it on GitHubhttps://github.com/jmcrosa/Anarchy_TPP_Ecruteak/issues/3#issuecomment-36771571 .

Jose Crosa

jmcrosa commented 10 years ago

I just pushed an update that addresses some of these issues. I did not use numpy, however I did change random.randint() to random.randchoice(), which I think it's more efficient. Also added an Odds class that takes over the states of the odds, thus reducing the amount of times the elif is executed. Can you see if the performance is better?