Merge pull request #20 from nineties/make_python_faster

make python version faster by replacing // with >>
This commit is contained in:
Koichi NAKAMURA 2021-12-06 22:30:13 +09:00 committed by GitHub
commit c8e87b1dbf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -19,6 +19,8 @@ MEMORY_SIZE = 0x10000
memory = array.array('i', [0]*MEMORY_SIZE) memory = array.array('i', [0]*MEMORY_SIZE)
CELL = memory.itemsize CELL = memory.itemsize
CELL_SHIFT = CELL.bit_length() - 1
STACK_SIZE = 0x100 STACK_SIZE = 0x100
RSTACK_SIZE = 0x100 RSTACK_SIZE = 0x100
@ -37,10 +39,10 @@ def align():
write(HERE_CELL, aligned(read(HERE_CELL))) write(HERE_CELL, aligned(read(HERE_CELL)))
def read(addr): def read(addr):
return memory[addr//CELL] return memory[addr >> CELL_SHIFT]
def write(addr, v): def write(addr, v):
memory[addr//CELL] = ctypes.c_int(v).value memory[addr >> CELL_SHIFT] = ctypes.c_int(v).value
def comma(v): def comma(v):
here = read(HERE_CELL) here = read(HERE_CELL)
@ -48,13 +50,13 @@ def comma(v):
write(HERE_CELL, here + CELL) write(HERE_CELL, here + CELL)
def read_byte(addr): def read_byte(addr):
i = addr // CELL i = addr >> CELL_SHIFT
m = (addr % CELL)*8 m = (addr % CELL)*8
v = memory[i] v = memory[i]
return ctypes.c_int8((v >> m) & 0xff).value # sign extension return ctypes.c_int8((v >> m) & 0xff).value # sign extension
def write_byte(addr, c): def write_byte(addr, c):
i = addr // CELL i = addr >> CELL_SHIFT
m = (addr % CELL)*8 m = (addr % CELL)*8
v = memory[i] v = memory[i]
memory[i] = (v & ~(0xff << m)) | (c&0xff) << m memory[i] = (v & ~(0xff << m)) | (c&0xff) << m
@ -277,7 +279,7 @@ add_simple_operator('(write)', writefile)
add_simple_operator('(read)', readfile) add_simple_operator('(read)', readfile)
def allocate(): def allocate():
size = pop() size = pop()
n = (size + CELL - 1) // CELL n = (size + CELL - 1) >> CELL_SHIFT
addr = len(memory) * CELL addr = len(memory) * CELL
memory.extend([0]*n) memory.extend([0]*n)
push(addr) push(addr)