Display Performance Improvement

So, I did some things in my game, and after adding some thing, the game became quite laggy. I did some tests using pygame.time.get_ticks () to find out where the time is spent in my loop, and found that about 90% of the time was spent in two places. 1. Drawing all my sprites on the screen. 2. Draw an ability manager that simply draws / blends some images.

I was embarrassed when I deleted my convert () and convert_alpha () functions, greatly improving the performance in my feature manager, and then removing the envelopes when drawing my sprites did not seem to affect the performance.

Does anyone have an idea why converting can slow things down, documents say this is the best way to go. Also, why can this help in one area and not in another?

Edit: some numbers to show my tests. Removing conversions for # 2, a feature manager drawing, reduced the average time to draw them from about 80 milliseconds to about 45 milliseconds. Removing or adding envelopes for # 1, drawing sprites on the screen, hardly affects the time to do something. Affects range from + or - 5. This small change may not be the result of deleting the converted files, so my question should mainly focus on “Why does it remove so much money for conversion in the graphical capabilities manager?”, And just a little bit about why this is can help so much in one area and not in another.

+4
source share
1 answer

: pygame, , .

: http://www.pygame.org/docs/ref/surface.html#pygame.Surface.convert

:

, , . blitting.

, , . [ ].

, .

, (.. , )

, . [, ,] .

blit, [] [C] memcpy, . ​​ . [ - 2D 6].

, , . , .

, -, - .

, , /, .


UPDATE:

, , , .

, 80 . , .

"" , [] (, ..).

, ?

, , , : 45 80. 22, . (, 30 ), , , , . , , N N + 1.

, , .

[] blit convert (.. - ).

, , blit_convert , ability_manager_surface.

, blit (, blit_fast blit_slow ). . , . , . .

blit_convert ability_manager_surface " " (, precalc_manager_surface), blit_fast precalc_manager_surface. "" .

# dstv -- destination pixel array
# dsthgt -- destination height
# dstwid -- destination width
#
# dstybase -- destination Y position for upper left corner of inset
# dstxbase -- destination X position for upper left corner of inset
#
# srcv -- source pixel array
# srchgt -- source height
# srcwid -- source width

# ------------------------------------------------------------------------------
# blit_fast -- fast blit
# this uses a 1 dimensional array to be fast
def blit_fast(dstv,dsthgt,dstwid,dstybase,dstxbase,srcv,srchgt,srcwid):

    # NOTE: I may have messed up the equations here
    for yoff in range(dstybase,dstybase + srchgt):
        dstypos = (yoff * dstwid) + dstxbase
        srcypos = (yoff * srcwid);

        for xoff in range(0,srcwid):
            dstv[dstypos + xoff] = srcv[srcypos + xoff]

# ------------------------------------------------------------------------------
# blit_slow -- slower blit
# this uses a 2 dimensional array to be more clear
def blit_slow(dstv,dsthgt,dstwid,dstybase,dstxbase,srcv,srchgt,srcwid):

    for yoff in range(0,srchgt):
        for xoff in range(0,srcwid):
            dstv[dstybase + yoff][dstxbase + xoff] = srcv[yoff][xoff]

# ------------------------------------------------------------------------------
# blit_convert -- blit with conversion
def blit_convert(dstv,dsthgt,dstwid,dstybase,dstxbase,srcv,srchgt,srcwid):

    for yoff in range(0,srchgt):
        for xoff in range(0,srcwid):
            dstv[dstybase + yoff][dstxbase + xoff] = convert(srcv,yoff,xoff)

# convert -- conversion function
# NOTE: this is more like a blur or soften filter
# the main point is this takes _more_ time than a simple blit
def convert(srcv,ypos,xpos):

    # we ignore the special case for the borders

    cur = srcv[ypos][xpos]

    top = srcv[ypos - 1][xpos]
    bot = srcv[ypos + 1][xpos]
    left = srcv[ypos][xpos - 1]
    right = srcv[ypos][xpos + 1]

    # do a [sample] convolution kernel
    # this equation probably isn't accurate -- just to illustrate something that
    # is computationally expensive on a per pixel basis
    out = (cur * 0.6) + (top * 0.1) + (bot * 0.1) + (left * 0.1) + (right * 0.1)

    return out

. "". / (, 1024x768 → 1920x1080), / " ", . , , . [ ]: https://cnx.org/contents/xOVdQmDl@10/Polyphase-Resampling-with-a-Ra


# 2:

,

. , . , .

, , , , .

, / .

, , blit, , ,

[]. ...

(, , .png), , . , .

, , [blit], . , - . , , ?

- . " ", pygame.display.flip .

, , .

[ ]: http://www.balloonbuilding.com/index.php?chapter=example_code

- " " " pygame". , [ ] , , .

+1

All Articles