On Wed, Oct 2, 2013 at 12:53 PM, F. B. <franz.bona...@gmail.com> wrote: > > On Wednesday, October 2, 2013 8:26:59 PM UTC+2, Ondřej Čertík wrote: >> >> The problem with Cython is that I am not able to make it as fast as >> CSymPy in C++, >> and I tried: >> >> https://github.com/certik/sympyx/blob/master/sympy_pyx.pyx >> >> The reason is that to truly nail the speed, one needs to use special >> dictionary implementation, >> tweak hashing functions, reference counting, memory allocators etc. >> C++ allows to play with all that, while >> having a reasonably high level syntax. In Cython one has to use Python >> types like hashes/dicts ---- or resort >> to effectively writing a pure C code --- either way, it becomes >> overall more complex than just writing >> things in C++ by hand. So I'll be happy to be proven wrong, but so far >> C++ is a clear winner in terms of speed. >> >> Besides speed, I came to appreciate having just one language and write >> everything in it. Much simpler to maintain >> and master. With your sympy2cython approach, you need to master >> Python, SymPy code, the sympy2cython >> tool, Cython, C and Python C/API intricacies. That's a lot of layers. >> The reason to do this is speed, otherwise >> we can just use SymPy --- but to nail speed, you need to master all >> these layers extremely well, and it might >> still not be enough --- i.e. I think I know Cython and Python C/API >> reasonably well, but I don't know how to >> make the Cython code above as fast as CSymPy. In C++, on the other >> hand, you can really nail the speed and it is >> just one language to master and maintain. >> >> Ondrej > > > OK, maybe for speed purposes C++ handwriting remains the best solution and > automated translators/code generators are not that good. > > What about still trying to force static typing inside selected parts of > SymPy? I mean, once static typing is enforced, it should become easier to > translate Python to C++ by hand, because one is not obliged to heavily alter > the algorithm in case of variables of ambiguous type.
I think once we stop supporting Python 2.7, we can start using static typing quite easily and I think it helps. Though I think one of the most useful algorithms in SymPy that are candidates for C++ rewrite are polynomials, and those as far as I know don't use much duck typing, so it should be fairly easy to rewrite using fast native C++ datastructures. For example, here is my implementation of sparse polynomial multiplication in C++: https://github.com/certik/csympy/blob/master/src/rings.cpp#L63 https://github.com/certik/csympy/blob/master/src/monomials.cpp#L7 and I think it might be possible to speed it up further by playing with the hashtable and hashes and similar things. I tried to optimize a hash function for this case already, see here: https://github.com/certik/csympy/blob/master/src/dict.h#L48 the idea is that the hash function needs to be fast to compute for the tuple of ints and also as unique as possible. Btw, I don't want to discourage you from trying things. I only wanted to share my own (hard-earned) experience, as I tried various approaches. For example, one great project would be to implement similar symbolic core in Julia. It might get competitive or even faster than my Cython version above. Whether it could match my C++ version, that I don't know. Ondrej -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscr...@googlegroups.com. To post to this group, send email to sympy@googlegroups.com. Visit this group at http://groups.google.com/group/sympy. For more options, visit https://groups.google.com/groups/opt_out.