Chris Angelico schrieb am 31.07.2015 um 09:37: > On Fri, Jul 31, 2015 at 5:26 PM, Stefan Behnel wrote: >> Your C code seems to be only about 1500 lines, not too late to translate >> it. That should save you a couple of hundred lines and at the same time >> make it work with Python 3 (which it currently doesn't, from what I see). > > To what extent does Cython make this easier? The biggest barrier I > would expect to see is the bytes/text distinction
Yes, that tends to be a barrier. Cython is mostly just Python, so you can write if isinstance(s, unicode): s = (<unicode> s).encode('utf8') and be happy with it ("<type>" is a cast in Cython). Such simple code looks uglier when spelled out using the C-API and wouldn't be any CPU cycle faster. But there's also the PyInt/PyLong unification, which can easily get in the way for a number processing library. In Cython, you can write if isinstance(x, (int, long)): try: c_long = <long> x except OverflowError: ... # do slow conversion of large integer here else: ... # do fast conversion from c_long here or something like that and it'll work in Py2.6 through Py3.5 because Cython does the necessary adaptations internally for you. This code snippet already has a substantially faster fast-path than what the OP's code does and it will still be much easier to tune later, in case you notice that the slow path is too slow after all. And then there are various helpful little features in the language like, say, C arrays assigning by value, or freelists for extension types using a decorator. The OP's code would clearly benefit from those, if only for readability. Python is much easier to write and maintain than C. Cython inherits that property and expands it across C data types. And it generates C code for you that automatically adapts to the different Python versions in various ways, both in terms of compatibility and performance. Stefan -- https://mail.python.org/mailman/listinfo/python-list