Writing an emulator in python - implementation questions (for performance)

Carl Banks pavlovevidence at gmail.com
Fri Nov 13 00:07:14 EST 2009


On Nov 12, 6:37 am, Santiago Romero <srom... at gmail.com> wrote:
> > >  I'm trying to port (just for fun), my old Sinclair Spectrum emulator,
> > > ASpectrum, from C to Python + pygame.
>
> > The answer to your question is, "Use numpy".  More details below.
>
>  Let's see :-)
>
> > >  How can I implement this in Python, I mean, define a 16 byte variable
> > > so that high and low bytes can be accessed separately and changing W,
> > > H or L affects the entire variable? I would like to avoid doing BIT
> > > masks to get or change HIGH or LOW parts of a variable and let the
> > > compiled code to do it by itself.
>
> > You can do clever memory slicing like this with numpy.  For instance:
>
> > breg = numpy.zeros((16,),numpy.uint8)
> > wreg = numpy.ndarray((8,),numpy.uint16,breg)
>
> > This causes breg and wreg to share the same 16 bytes of memory.  You
> > can define constants to access specific registers:
>
> > R1L = 1
> > R1H = 2
> > R1 = 1
>
> > breg[R1H] = 2
> > print wreg[R1]
>
>  And how about speed?
>
> Assuming a 16 bit register named BC which contains 2 8 bit regiters (B
> and C)...
>
>  Will the above be faster than shifts and bit operations (<<, and,>> ) with new B and C values to "recalculate" BC when reading or
>
> changing either B, C or BC?

I don't know.  Strange thing about Python, the community generally
isn't hellbent on speed so we don't usually memorize a bunch of facts
like "X is faster than Y".  I'm personally aware of only a few general
rules of thumb on speed (like "local variables are much faster than
globals" and "function calls have rather high overhead").

Obviously, given the problem you chose, you know you're going to have
to worry about speed from the beginning.  But since not many of us
know speed details off-hand, you'll probably get a faster answer by
just running the speed tests yourself.

Having said that, I *suspect* the numpy approach I mentioned will be
quite a bit faster than using bit operations and recalculating BC, but
I really don't know.


Carl Banks



More information about the Python-list mailing list