Hello,<br><br>I'm a bit new to pyrex and am not too clear about how dictionaries<br>are handled. I'm using a number of python dictionaries where the<br>indices (keys) and the values are 64 bit numbers (from 0 to (1<<64) - 1).
<br>(i.e. unsigned long long...U64).<br>While the arrays contain (and are indexed by) U64 numbers, they do not<br>contain that many values (no more then 64x256 entries). <br><br>I'd like to move the most CPU intensive portions of my code to pyrex
<br>modules but I'm not quite sure how to do this. I know that one can create<br>unsigned long long variables, but how should the dictionaries be declared and created?<br>And where are they declared and created? In python or the pyrex functions? From what
<br>I can gather, any access to a dictionary (whether created in python or pyrex<br>code) slows everything down to pure python speeds.<br><br>This is a small sample of what I'm trying to do:<br>Most of the time is spent in functions like get_move
<br>and these functions need to access dictionaries made up of<br>these 64 bit numbers.<br><br>Any help would be appreciated.<br><br>Thanks,<br>Sam<br><br>---------------------------<br>Python<br>=====<br>rank = {}<br>rank_mask = {}
<br>count = 0<br>rank[0] = 0<br>rank_mask[0] = 0<br>for i in range(1,9):<br> for j in range(8,0,-1):<br> rank[1L<<count] = i<br> rank_mask[1L<<count] = (255L<<8*(i-1))<br> count = count + 1
<br><br>rank_attacks = {} # long complicated definition...<br>rank_attacks[1<<45] = {}<br>rank_attacks[1<<45][0] = 0<br><br><br>piece = 1<<45 <br>all_pieces = 1<<35 | 1<<33<br>other_pieces = 1<<62
<br><br>print sample1.get_move(piece,all_pieces,other_pieces,<br> rank_mask,rank_attacks)<br><br><br>Pyrex<br>====<br>def get_move(unsigned long long piece, unsigned long long all_pieces,<br> unsigned long long other_pieces, rank_mask, rank_attacks):
<br><br> cdef unsigned long long from_square<br> cdef unsigned long long rank_pieces<br> cdef unsigned long long to_square<br><br> mymoves = []<br><br> while (piece):<br> from_square = piece & -piece
<br> # handle the rank attacks<br> rank_pieces = rank_mask[from_square] & all_pieces<br> # this produces all moves including our own piece attacks<br> moves = (rank_attacks[from_square][rank_pieces] & other_pieces) | \
<br> (rank_attacks[from_square][rank_pieces] & ~all_pieces) <br><br><br> while(moves):<br> to_square = moves & -moves<br> mymoves.append[(from_square,to_square)]<br> moves = moves & (moves - 1)
<br> <br> piece = piece & (piece - 1)<br><br> return moves<br><br><br>---------------------------------------<br>