Go和Cython之间的差异

Go和Cython之间的差异

问题描述:

Today a great friend of mine asked me what are the main differences between the newest Go language and Cython, which is a set of C-extensions for Python. I don't have much knowledge on Python, can anyone tell me why Go is better/worse than Cython?

今天,我的一个好朋友问我最新的Go语言和 Cython ,它是Python的一组C扩展。 我对Python并不了解,有人可以告诉我为什么Go比Cython更好/更糟糕吗? p> div>

Cython isn't really a language in the conventional sense. It is a preprocessor for building Python extensions that takes Python-like syntax (actually they strive for full Python compatibility) and produces C code (using the Python C API). Doing this they are able to include some special case optimisations, but the real benefits come when you add Cython specific static type information which is incorporated into the C code, bypassing the Python runtime for those operations and resulting in a high speed up.

Go is a compiled programming language. The first thing that can be done in Go is producing an executable that doesn't include the Python runtime/start a Python interpreter - this is impossible in Cython. (May not be technically impossible - but there is really no point to use Cython if you are not working with Python). Since Cython just produces C most of your questions in the comment don't really apply - you can use any C debugger (although the fact that's a Python extension makes things a bit more complicated).

GO introduces goroutines and channels. See language FAQ

Differences? Pretty much everything!

  • Concurrency and channels.
  • Interfaces.
  • Static typechecking.
  • ...

My main reason for trying out go is the supposed ease of introducing concurrency into programs. I think that will be the 'next big thing', as processor speeds will tail off, and increasingly multiple cores are available. If you want to make use of multicore processors, you need to write your program so that it can run things concurrently.

I earlier looked at Erlang, but despite being used to Prolog I find it a bit strange still; it is so different from your 'average' programming language (of the C or Pascal family). But its concurrency features are easy to use, once you get the hang of it. With very little effort I was able to write a parallel parser, which does not use a stack, but spawns a new 'thread/process' every time there were multiple options.

So far go looks quite alright, despite some slight inconsistencies. And it's also fast, which is a bonus.

So unless Cython also makes concurrency easy, I'd favour go...

gevent is a concurrent library that uses Cython at its core. It seems to be pretty fast: http://nichol.as/asynchronous-servers-in-python

What about support. You are relying on a single compiler, provided by Google. What if Go folds or goes commercial?

With Cython you could always go back to Python (or port the C code) if the Cython project folded.

UPDATE: I must say that I am now upset with Cython. The lack of thread support is a major blow. Cython is thread-safe BUT at a serious cost. The global interpreter lock is held the whole time a function executes. Thereby disabling concurrent execution over an entire codebase!

Cython's C-like features are poorly documented and confusing to novices. I admit.

Cython's purpose is to support the Sage mathematics software; Go's is to support Google's ambitious plans for cutting-edge expensive $$ hardware.

In short, I no longer like either one of these languages. Going back to C++ (again). My favorite is Cython.