Profiling your Renpygame code for performance.

In this forum we discuss the future of Ren'Py, both bug fixes and longer-term development. Pre-releases are announced and discussed here.
Post Reply
Message
Author
User avatar
DizzyKa
Newbie
Posts: 15
Joined: Sat Sep 24, 2016 4:49 pm
Projects: @GossamerAcademy
Contact:

Profiling your Renpygame code for performance.

#1 Post by DizzyKa »

Hey all,

Not sure if this is the right place, but I wanted to post a possibly helpful tidbit if you're using Renpygame to do a complex simulation. If your game is running slow and you want to figure out where most of the time is spent each frame you can use some standard profiling tools called 'cProfile' and 'profile'. Pygame has a page that talks about how to profile your code here : http://www.pygame.org/wiki/Profiling?pa ... ok%3Cbr%3E

However, with the base install of Renpy there are some files missing so you can't do this right out of the box. Here's how to get profiling up and running:

Install Python2.7 somewhere on your machine if it is not already installed and navigate to the installation directory. Copy the files 'pstats.py', 'profile.py', and 'cprofile.py' from your Python27 installation directory into 'your_installation_directory\renpy-6.99.10-sdk\renpy-6.99.10-sdk\lib\pythonlib2.7\'.

You're good to go from here, using this documentation to show you the way: https://docs.python.org/2/library/profile.html

Here's an example of how you might set up profiling in your program:

Code: Select all

import pstats
import profile

def mainGameLoop():
    # Your game code here.

def someStartFunction():
    # Calling profile.runctx instead of profile.run is probably necessary because the profile module gets confused about its context
    # when running within Renpy and Pygame.
    profile.runctx('mainGameLoop()', globals(), locals(), 'NameOfBinaryOutputFile.prof')

    # If you just have the line of code above, the profiling output will be saved as a binary file which isn't very useful to humans.
    # These few lines convert the binary output into text output you can actually read and interact with.
    stream = open('NameOfHumanReadableTextOutputFile.txt', 'w')
    stats = pstats.Stats('NameOfBinaryOutputFile.prof', stream=stream)

    # This line sorts the profiler output based on column headings you provide.
    # With what I've provided below it sorts by number of calls and total time spent in a function.
    stats.sort_stats('ncalls','tottime')

    # Bam! Done. Check your Renpy install directory for the text file.
    stats.print_stats()
Sample output looks like this:

Code: Select all

Wed Jan 18 16:21:07 2017    MTDProfBinary.prof

   483590 function calls (479038 primitive calls) in 53.850 seconds

   Ordered by: call count, internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    94478    0.057    0.000    0.057    0.000 beatpy.py:1449(<lambda>)
50527/45992    0.289    0.000   15.437    0.000 beatpy.py:456(updateObject)
    45992    0.027    0.000    0.027    0.000 beatpy.py:155(updateObject)
    38881   51.273    0.001   51.273    0.001 :0(blit)
    20805    0.044    0.000    0.070    0.000 :0(get)
    16374    0.009    0.000    0.009    0.000 U:\Projects\trunk\renpy-6.99.10-sdk\renpy-6.99.10-sdk\renpy\game.py:225(context)
Thats it! Hope that's helpful to someone.

Post Reply

Who is online

Users browsing this forum: No registered users