Skip to main content
edited title; removed follow-up
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Python - Finding surrounding tiles for loop performanceall tiles

EDIT

I wrote the following, which is around 10x faster than the original one.

Where width is the width of the map, and height is the height of the map. Tiles are appended to the tiles list first from right to left and then from bottom to up.

for t in tiles:
        index = tiles.index(t)
        if index > width - 1:
            t.surroundings.append(tiles[index - width])
            if not index % width == width - 1:
                t.surroundings.append(tiles[index - width + 1])
            if not index % width == 0:
                t.surroundings.append(tiles[index - width - 1])
        if not index % width == width - 1:
            t.surroundings.append(tiles[index + 1])
        if not index % width == 0:
            t.surroundings.append(tiles[index - 1])
        if index < width * height - width:
            t.surroundings.append(tiles[index + width])
            if not index % width == width - 1:
                t.surroundings.append(tiles[index + width + 1])
            if not index % width == 0:
                t.surroundings.append(tiles[index + width - 1])

Thanks for help everyone! I would check all the answers accepted if I could.

Python - for loop performance

EDIT

I wrote the following, which is around 10x faster than the original one.

Where width is the width of the map, and height is the height of the map. Tiles are appended to the tiles list first from right to left and then from bottom to up.

for t in tiles:
        index = tiles.index(t)
        if index > width - 1:
            t.surroundings.append(tiles[index - width])
            if not index % width == width - 1:
                t.surroundings.append(tiles[index - width + 1])
            if not index % width == 0:
                t.surroundings.append(tiles[index - width - 1])
        if not index % width == width - 1:
            t.surroundings.append(tiles[index + 1])
        if not index % width == 0:
            t.surroundings.append(tiles[index - 1])
        if index < width * height - width:
            t.surroundings.append(tiles[index + width])
            if not index % width == width - 1:
                t.surroundings.append(tiles[index + width + 1])
            if not index % width == 0:
                t.surroundings.append(tiles[index + width - 1])

Thanks for help everyone! I would check all the answers accepted if I could.

Finding surrounding tiles for all tiles

added 93 characters in body
Source Link

Where width is the width of the map, and height is the height of the map. Tiles are appended to the tiles list first from right to left and then from bottom to up.

Where width is the width of the map, and height is the height of the map.

Where width is the width of the map, and height is the height of the map. Tiles are appended to the tiles list first from right to left and then from bottom to up.

added 945 characters in body
Source Link

EDIT

I wrote the following, which is around 10x faster than the original one.

Where width is the width of the map, and height is the height of the map.

for t in tiles:
        index = tiles.index(t)
        if index > width - 1:
            t.surroundings.append(tiles[index - width])
            if not index % width == width - 1:
                t.surroundings.append(tiles[index - width + 1])
            if not index % width == 0:
                t.surroundings.append(tiles[index - width - 1])
        if not index % width == width - 1:
            t.surroundings.append(tiles[index + 1])
        if not index % width == 0:
            t.surroundings.append(tiles[index - 1])
        if index < width * height - width:
            t.surroundings.append(tiles[index + width])
            if not index % width == width - 1:
                t.surroundings.append(tiles[index + width + 1])
            if not index % width == 0:
                t.surroundings.append(tiles[index + width - 1])

Thanks for help everyone! I would check all the answers accepted if I could.

EDIT

I wrote the following, which is around 10x faster than the original one.

Where width is the width of the map, and height is the height of the map.

for t in tiles:
        index = tiles.index(t)
        if index > width - 1:
            t.surroundings.append(tiles[index - width])
            if not index % width == width - 1:
                t.surroundings.append(tiles[index - width + 1])
            if not index % width == 0:
                t.surroundings.append(tiles[index - width - 1])
        if not index % width == width - 1:
            t.surroundings.append(tiles[index + 1])
        if not index % width == 0:
            t.surroundings.append(tiles[index - 1])
        if index < width * height - width:
            t.surroundings.append(tiles[index + width])
            if not index % width == width - 1:
                t.surroundings.append(tiles[index + width + 1])
            if not index % width == 0:
                t.surroundings.append(tiles[index + width - 1])

Thanks for help everyone! I would check all the answers accepted if I could.

Tweeted twitter.com/#!/StackCodeReview/status/261618652443463681
Source Link
Loading