I built on what you had to get a float -> float hashtable. Note that this technique only works since the float value fits inside a void*, anything bigger and you'd have to allocate memory manually to stick it into the hashtable.
- Robert
cy_stl.pyx
Description: Binary data
sage: from cy_stl import * sage: time time_c_hashtable(10**5) CPU times: user 0.01 s, sys: 0.00 s, total: 0.01 s Wall time: 0.01 s sage: time time_c_hashtable(10**6) CPU times: user 0.08 s, sys: 0.00 s, total: 0.08 s Wall time: 0.08 s sage: time time_c_hashtable(10**7) CPU times: user 0.75 s, sys: 0.00 s, total: 0.75 s Wall time: 0.75 s sage: time time_py_hashtable(10**5) CPU times: user 0.02 s, sys: 0.00 s, total: 0.02 s Wall time: 0.02 s sage: time time_py_hashtable(10**6) CPU times: user 0.18 s, sys: 0.00 s, total: 0.19 s Wall time: 0.19 s sage: time time_py_hashtable(10**7) CPU times: user 2.01 s, sys: 0.01 s, total: 2.02 s Wall time: 2.02 s Not near the speed gains I was expecting...disappointing. - Robert On Sep 7, 2009, at 12:28 PM, Sebastien Binet wrote:
hi there,attached is a simple cy_stl.pyx file (together with its setup.py companion)really just to get started :) to test: $ python -c 'import cy_stl as cc; cc.test()' hth, sebastien.cythonHash.pxd: cdef extern from "hash-table.h": ctypedef struct HashTable ctypedef void *HashTableKey ctypedef unsigned long HashTableHashFunc(HashTableKey value) ctypedef unsigned long HashTableEqualFunc(HashTableKey value) HashTable *hash_table_new(HashTableHashFunc hash_func, HashTableEqualFunc equal_func) cdef inline unsigned long c_hash_func(HashTableKey value): return 1 cdef inline unsigned long c_hash_equal(HashTableKey value): return 1 cythonPT.pyx: cimport cythonHash from cythonHash cimport HashTable class MY_Phrase_Table(object): def __init__(self): pp = HashTable #error here print type(pp), ppThis yields in the following error: 'HashTable' is not a constant, variableor function identifier.I don't really get how I should reference to Hashtable. I thought it wasalready declared from .pxd and the original .c file. -----Original Message----- From: Stefan Behnel [mailto:[email protected]] Sent: vrijdag 4 september 2009 16:03 To: [email protected] Cc: [email protected] Subject: Re: [Cython] FW: cython and hash tables / dictionary Sanne Korzec wrote:In the documentationhttp://c-algorithms.sourceforge.net/doc/hash- table_8h.html#e361c4c0256ec6c741ecfeabef33d891 , I can find: HashTable* hash_table_new ( HashTableHashFunc hash_func, HashTableEqualFunc equal_func )To create a new hash table. But I can't find were the HashTableHashFuncandHashTableEqualFunc are declared. The only thing I can find is in theheaderfile which state: typedef unsigned long(* HashTableHashFunc)(HashTableKey value) typedef unsigned long(* HashTableHashFunc)(HashTableKey value) Does this mean I have to write these functions myself?Yes.In c?You can write them in Cython: cdef unsigned long c_hash(HashTableKey value): return huge_calculation_on(value)And how then do I call them from cython?You don't. Instead, you pass the function names (i.e. pointers) into hash_table_new().My guess: hashtable.pyx"hashtable.pxd", I assume?cdef extern from "hash_table.h":object HashTable hash_table_new(object hash_func, object equal_func)That won't work. You can't use Python functions as their signature won't match the required signatures. Instead, define HashTable as a struct andthe functions as a ctypedef. Stefan _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev-- ######################################### # Dr. Sebastien Binet # Laboratoire de l'Accelerateur Lineaire # Universite Paris-Sud XI # Batiment 200 # 91898 Orsay#########################################<cy_stl.pyx><setup.py>_______ ________________________________________Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
_______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
