Giter VIP home page Giter VIP logo

duga's People

Contributors

maxwellsalmon avatar

duga's Issues

`Raycast.cast` is the bottleneck

I've been tinkering with profiling the code and I've found that the call to Raycast.cast slows down the game a lot-- dwarfing the slowdown from the software rendering that pygame does (as opposed to being done on the GPU like with OpenGL). The FPS tripled when I commented out the call to Raycast.cast in Raycast.calculate.

The environment I made the tests in are as follows:

  • (Roughly) 30 seconds of (wall-clock) timing from when the game opens and starts rendering (doing "normal" things in the game-- walking around, attacking, getting hit, opening doors, etc.)
  • Relevant specs:
    • CPU: Intel Core i7-4700MQ CPU @ 2.40 GHz
    • Ram: ~8 GB

Taking these into account: for improving the performance, I'd suggest focussing any future optimisation efforts on this function. That could mean implementing said function in C or some other faster language.

Initial profiling results

main_loop (average FPS - 30):

*** PROFILER RESULTS ***
main_loop (MAIN.py:274)
function called 1 times

         14194329 function calls in 32.651 seconds

   Ordered by: cumulative time, internal time, call count
   List reduced from 110 to 40 due to restriction <40>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.038    0.038   32.651   32.651 MAIN.py:274(main_loop)
      831    0.354    0.000   15.256    0.018 RAYCAST.py:80(calculate)
   117171    8.291    0.000   14.466    0.000 RAYCAST.py:130(cast)
      831    0.778    0.001   10.324    0.012 MAIN.py:198(render_screen)
   178092    5.407    0.000    5.407    0.000 {method 'blit' of 'pygame.Surface' objects}
   117171    0.208    0.000    5.369    0.000 RAYCAST.py:299(control)
   117171    0.667    0.000    5.121    0.000 RAYCAST.py:310(render_screen)
      831    0.014    0.000    4.787    0.006 MAIN.py:254(update_game)
     7479    0.073    0.000    4.766    0.001 NPC.py:111(think)
      188    0.030    0.000    2.987    0.016 PATHFINDING.py:13(pathfind)
     1281    0.007    0.000    2.888    0.002 NPC.py:551(attack)
   445416    1.327    0.000    2.168    0.000 MAIN.py:178(sort_atan)
   116874    0.973    0.000    2.073    0.000 RAYCAST.py:12(__init__)
     4185    0.041    0.000    1.751    0.000 NPC.py:423(move)
   116874    0.473    0.000    1.594    0.000 RAYCAST.py:25(update_rect)
   167718    1.594    0.000    1.594    0.000 {built-in method pygame.transform.scale}
     1662    0.150    0.000    1.452    0.001 {built-in method builtins.sorted}
    50844    0.473    0.000    1.440    0.000 SPRITES.py:36(get_pos)
   222708    0.155    0.000    1.261    0.000 MAIN.py:208(<lambda>)
     4714    0.067    0.000    1.242    0.000 NPC.py:389(collide_update)
   286841    1.171    0.000    1.171    0.000 {method 'convert_alpha' of 'pygame.Surface' objects}
     4714    0.288    0.000    1.029    0.000 NPC.py:401(<listcomp>)
  1840915    0.824    0.000    0.824    0.000 {method 'colliderect' of 'pygame.Rect' objects}
      832    0.760    0.001    0.760    0.001 {built-in method pygame.event.get}
    54755    0.685    0.000    0.685    0.000 {method 'fill' of 'pygame.Surface' objects}
     2305    0.684    0.000    0.684    0.000 PATHFINDING.py:69(<listcomp>)
     2305    0.671    0.000    0.671    0.000 PATHFINDING.py:71(<listcomp>)
   891584    0.480    0.000    0.600    0.000 {built-in method builtins.max}
     2305    0.599    0.000    0.599    0.000 PATHFINDING.py:70(<listcomp>)
     2305    0.594    0.000    0.594    0.000 PATHFINDING.py:68(<listcomp>)
   293418    0.447    0.000    0.561    0.000 MAP.py:106(get_dist)
      831    0.453    0.001    0.453    0.001 {built-in method pygame.display.update}
      831    0.008    0.000    0.387    0.000 MAIN.py:155(draw)
   116874    0.339    0.000    0.339    0.000 {method 'convert' of 'pygame.Surface' objects}
      631    0.011    0.000    0.321    0.001 NPC.py:336(detect_player)
     1466    0.308    0.000    0.308    0.000 NPC.py:375(<listcomp>)
      678    0.013    0.000    0.306    0.000 GUNS.py:244(draw)
   893139    0.292    0.000    0.296    0.000 {built-in method builtins.min}
      832    0.037    0.000    0.288    0.000 PLAYER.py:54(control)
        1    0.280    0.280    0.280    0.280 {built-in method pygame.base.quit}

Raycast.calculate (average FPS - 25):

*** PROFILER RESULTS ***
calculate (...\DUGA\RAYCAST.py:82)
function called 973 times

         7788432 function calls in 17.309 seconds

   Ordered by: cumulative time, internal time, call count

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
      973    0.439    0.000   17.308    0.018 RAYCAST.py:82(calculate)
   137193    8.702    0.000   16.320    0.000 RAYCAST.py:133(cast)
   137193    0.249    0.000    6.694    0.000 RAYCAST.py:302(control)
   137193    0.792    0.000    6.399    0.000 RAYCAST.py:313(render_screen)
   136019    1.164    0.000    2.445    0.000 RAYCAST.py:14(__init__)
   136019    0.614    0.000    2.035    0.000 RAYCAST.py:27(update_rect)
   326048    1.383    0.000    1.383    0.000 {method 'convert_alpha' of 'pygame.Surface' objects}
   136019    1.041    0.000    1.041    0.000 {built-in method pygame.transform.scale}
   281197    0.430    0.000    0.526    0.000 MAP.py:106(get_dist)
    54010    0.489    0.000    0.489    0.000 {method 'fill' of 'pygame.Surface' objects}
   136019    0.397    0.000    0.397    0.000 {method 'convert' of 'pygame.Surface' objects}
   188238    0.240    0.000    0.263    0.000 RAYCAST.py:112(find_offset)
   998051    0.241    0.000    0.241    0.000 {built-in method math.cos}
   822454    0.225    0.000    0.225    0.000 {built-in method math.tan}
  1820505    0.208    0.000    0.208    0.000 {built-in method math.radians}
   136019    0.201    0.000    0.201    0.000 {method 'subsurface' of 'pygame.Surface' objects}
   272038    0.156    0.000    0.156    0.000 {method 'get_rect' of 'pygame.Surface' objects}
  1187463    0.135    0.000    0.135    0.000 {built-in method builtins.abs}
   281197    0.095    0.000    0.095    0.000 {built-in method math.sqrt}
   326048    0.076    0.000    0.076    0.000 {method 'get_size' of 'pygame.Surface' objects}
   137193    0.030    0.000    0.030    0.000 {method 'append' of 'list' objects}
      364    0.001    0.000    0.001    0.000 MAP.py:117(sesam_luk_dig_op)
      973    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        6    0.000    0.000    0.000    0.000 {method 'colliderect' of 'pygame.Rect' objects}
        0    0.000             0.000          profile:0(profiler)

main_loop with the Raycast.calculate call commented out (average FPS - 60):

*** PROFILER RESULTS ***
main_loop (MAIN.py:274)
function called 1 times

         23519372 function calls in 31.220 seconds

   Ordered by: cumulative time, internal time, call count
   List reduced from 114 to 40 due to restriction <40>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.079    0.079   31.220   31.220 MAIN.py:274(main_loop)
     1783    0.457    0.000   13.697    0.008 MAIN.py:198(render_screen)
     1783    0.052    0.000   11.230    0.006 MAIN.py:254(update_game)
    24879    0.242    0.000   11.152    0.000 NPC.py:111(think)
    14643    0.150    0.000    9.797    0.001 NPC.py:423(move)
    18047    0.456    0.000    8.724    0.000 NPC.py:389(collide_update)
    18047    2.022    0.000    7.141    0.000 NPC.py:401(<listcomp>)
 11393243    5.545    0.000    5.545    0.000 {method 'colliderect' of 'pygame.Rect' objects}
     3566    0.483    0.000    5.349    0.002 {built-in method builtins.sorted}
   199883    1.858    0.000    5.045    0.000 SPRITES.py:36(get_pos)
   850491    0.606    0.000    4.846    0.000 MAIN.py:208(<lambda>)
   905669    2.877    0.000    4.510    0.000 MAIN.py:178(sort_atan)
   199884    2.823    0.000    2.823    0.000 {built-in method pygame.transform.scale}
    19768    2.277    0.000    2.277    0.000 {method 'blit' of 'pygame.Surface' objects}
     1784    2.042    0.001    2.042    0.001 {built-in method pygame.event.get}
     2262    0.011    0.000    1.647    0.001 NPC.py:551(attack)
      141    0.015    0.000    1.441    0.010 PATHFINDING.py:13(pathfind)
     1784    0.103    0.000    1.298    0.001 PLAYER.py:54(control)
     1783    1.172    0.001    1.172    0.001 {built-in method pygame.display.update}
     2502    0.011    0.000    1.151    0.000 PLAYER.py:222(move)
     5004    0.057    0.000    1.140    0.000 PLAYER.py:229(update)
     1783    0.025    0.000    1.139    0.001 MAIN.py:155(draw)
   559457    0.860    0.000    1.120    0.000 MAP.py:106(get_dist)
     5004    0.013    0.000    1.083    0.000 sprite.py:1482(spritecollide)
     5004    0.672    0.000    1.070    0.000 sprite.py:1525(<listcomp>)
     3610    0.820    0.000    0.820    0.000 {method 'fill' of 'pygame.Surface' objects}
  1811902    0.665    0.000    0.760    0.000 {built-in method builtins.max}
  1812335    0.523    0.000    0.525    0.000 {built-in method builtins.min}
  1109232    0.494    0.000    0.494    0.000 {built-in method math.atan2}
    10216    0.010    0.000    0.453    0.000 SPRITES.py:84(draw)
        1    0.390    0.390    0.390    0.390 {built-in method pygame.base.quit}
     1783    0.029    0.000    0.372    0.000 HUD.py:21(render)
     1783    0.026    0.000    0.326    0.000 EFFECTS.py:12(render)
      997    0.300    0.000    0.300    0.000 PATHFINDING.py:69(<listcomp>)
     1783    0.298    0.000    0.298    0.000 {built-in method pygame.draw.rect}
      777    0.013    0.000    0.295    0.000 NPC.py:336(detect_player)
      997    0.292    0.000    0.292    0.000 PATHFINDING.py:71(<listcomp>)
      593    0.011    0.000    0.288    0.000 GUNS.py:244(draw)
     1122    0.280    0.000    0.280    0.000 NPC.py:375(<listcomp>)
   784219    0.273    0.000    0.273    0.000 {built-in method math.sqrt}

Raycast.calculate with the Raycast.cast call commented out (average FPS - 90):

*** PROFILER RESULTS ***
calculate (...\DUGA\RAYCAST.py:82)
function called 2771 times

         1862125 function calls in 2.257 seconds

   Ordered by: cumulative time, internal time, call count

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     2771    0.791    0.000    2.256    0.001 RAYCAST.py:82(calculate)
   731544    1.171    0.000    1.431    0.000 MAP.py:106(get_dist)
   731544    0.254    0.000    0.254    0.000 {built-in method math.sqrt}
   390711    0.035    0.000    0.035    0.000 {built-in method builtins.abs}
      884    0.005    0.000    0.006    0.000 MAP.py:117(sesam_luk_dig_op)
     2771    0.001    0.000    0.001    0.000 {method 'disable' of '_lsprof.Profiler' objects}
     1900    0.001    0.000    0.001    0.000 {method 'colliderect' of 'pygame.Rect' objects}
        0    0.000             0.000          profile:0(profiler)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.