Skip to content

Instantly share code, notes, and snippets.

@HexDecimal
Last active November 2, 2020 19:22
Show Gist options
  • Save HexDecimal/e25d39efc5f44d91f1003a738841b103 to your computer and use it in GitHub Desktop.
Save HexDecimal/e25d39efc5f44d91f1003a738841b103 to your computer and use it in GitHub Desktop.
Benchmarks for setting map attributes in python-tcod.
"""Python-tcod map attribute benchmarks for pytest-benchmark.
This test should be run with the Python optimize flag set.
Not doing so will slow down tcod.map_set_properties since it will emit
warnings on every call.
"""
import tcod
import numpy as np
tile_list = [
[{"transparent": True, "walkable": True} for _ in range(32)] for _ in range(32)
]
tile_array = np.ones((32, 32), dtype=[("transparent", bool), ("walkable", bool)])
def test_map_set_props_typical(benchmark):
def test(map_, tiles):
for y in range(32):
for x in range(32):
tcod.map_set_properties(
map_, x, y, tile_list[y][x]["transparent"], False
)
benchmark(test, tcod.map.Map(32, 32), tile_list)
def test_map_set_props_enumerate(benchmark):
def test(map_, tiles):
for y, row in enumerate(tiles):
for x, tile in enumerate(row):
tcod.map_set_properties(map_, x, y, tile["transparent"], False)
benchmark(test, tcod.map.Map(32, 32), tile_list)
def test_map_array_double_subscript(benchmark):
def test(map_, tiles):
for y, row in enumerate(tiles):
for x, tile in enumerate(row):
map_.transparent[y][x] = tile["transparent"]
benchmark(test, tcod.map.Map(32, 32), tile_list)
def test_map_array_subscript(benchmark):
def test(map_, tiles):
for y, row in enumerate(tiles):
for x, tile in enumerate(row):
map_.transparent[y, x] = tile["transparent"]
benchmark(test, tcod.map.Map(32, 32), tile_list)
def test_map_array_reference_subscript(benchmark):
def test(map_, tiles):
transparent = map_.transparent
for y, row in enumerate(tiles):
for x, tile in enumerate(row):
transparent[y, x] = tile["transparent"]
benchmark(test, tcod.map.Map(32, 32), tile_list)
def test_map_array_list_comprehension(benchmark):
def test(map_, tiles):
map_.transparent[:] = [[t["transparent"] for t in row] for row in tiles]
benchmark(test, tcod.map.Map(32, 32), tile_list)
def test_map_array_copy(benchmark):
def test(map_, tiles):
map_.transparent[:] = tiles["transparent"]
benchmark(test, tcod.map.Map(32, 32), tile_array)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment