Converting a bidimensional list in a bidimensional array
Scott David Daniels
Scott.Daniels at Acm.Org
Thu Jan 10 00:22:35 EST 2008
Santiago Romero wrote:
>...
[I wrote]
>> def __init__( self, bw, bh, tiles ):
>> self.width, self.height = bw, bh
>> self.tilemap = array.array('b', [0]) * bw * bh
>> Gives a pure linearization (you do the math for lines).
> Do you mean : tilemap[(width*y)+x] ?
Yup, exactly.
...
> What do you think about:
> - Memory Performance: Of course, my maps will take ( 16 bytes / 2-4
> bytes ) = 8 or 4 times less memory (32 / 64 bit processores) ...
> right?
How many distinct values do you have? If you have under a hundred (and
they are all positive), the actual integers are shared, so a byte array
only saves you at 4:1 (or 8:1 on a 64-bit processor).
> - Speed Performance: Do you think that changing from list to Array()
> would improve speed? I'm going to do lots of tilemap[y][x] checks (I
> mean, player jumping around the screen, checking if it's falling over
> a non-zero tile, and so).
The Pythonic answer to this is, "try it both ways." Don't intuit;
measure. Now if you are as old-school as I am, you start by thinking,
"<invective expurgated>, I don't want to spend a week writing and
running benchmarks."
Joy, Python excels here. at the command prompt (in 2.5, at least),
[a single line, I've broken it to three for newsgroup reading only]
C:\> \python25\python -m -s "import array; a = [array.array('b',
[0]*1000) for n in range(100)]"
"v = a[1][2] + a[2][1] + a[3][3]"
You can also use:
C:\> \python25\python -m -s "import m; m.setup()" "m.my_fun(123)"
Try it, you'll be addicted in no time. Check the documentation on
package "timeit."
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list