Go/python/erlang programming language comparison analysis and examples

Source: Internet
Author: User
Tags benchmark

This article is mainly about go, from the perspective of comparative analysis of language. The reason why I chose to compare with Python and Erlang is that it is a high-level language, they have a large similarity in language characteristics, but the main reason is that I am more familiar with them.

Many of the language features of Go are drawn from its three ancestors: C,pascal and CSP. Go's grammar, data type, control flow, etc. inherit from C,go's package, face objects and other ideas from the Pascal Branch, and go the largest language features, based on the pipeline communication of the co-process concurrency model, then learn from the CSP branch.

Comparison of language characteristics of Go/python/erlang

As the text of programming languages and Paradigms says, no matter how the language emerges, all languages are designed with 2 fundamentals: control flow and data type. In order to improve the language description ability, language generally provides control abstraction and data abstraction. The comparison of language features in this section also starts with these 4 dimensions, see (see larger image).

As we can see in the figure, go has only 31, compared to the 40 features of Python, which can be said to be quite restrained in language design. For example, it has no implicit numeric conversions, no constructors and destructors, no operator overloads, no default arguments, no inheritance, no generics, no exceptions, no macros, no function decorations, and no thread-local storage.

But the features of Go are also very distinct, for example, it has a co-process, automatic garbage collection, package management system, the function of the class citizen, stack space management.

Go, as a static type language, guarantees that go is better at running efficiency, memory usage, and type safety than Python and Erlang.

The data type of Go is also richer, in addition to supporting tables, dictionaries and other complex data structures, but also support pointers and interface types, which is not available in Python and Erlang. In particular, the interface type is particularly powerful, and it provides the means to manage the type system. The pointer type provides a means of managing memory, which provides strong support for go into the underlying software development.

Go has done a lot of introspection and trade-offs in the face of the object's feature support, it has no class, virtual function, inheritance, generics and other characteristics. The core of object-oriented programming in the go language is the combination and method (function). Composition is similar to the C language of the struct structure of the composition of the method, similar to Java Interface (Interface), but the use of methods with the object more decoupled, reducing the internal intrusion of the object. Erlang does not support the object programming paradigm, and Python's support for the object paradigm is most comprehensive in comparison.

Erlang, as a functional language, supports the most comprehensive features of functional programming. But basic functional language features, such as lambda, high-order functions, curry, etc., are supported in three languages.

The characteristics of the control flow support, three languages are similar. Erlang supports tail-recursive optimization, which facilitates functional programming. And go supports deep recursive calls by dynamically extending the stacks. Python is often exploded on deep recursive calls.

The concurrency model for go and Erlang comes from CSP, but Erlang is a concurrency model based on actor and message passing (mailbox), and go is based on Goroutine and pipeline (channel) concurrency. Regardless of Erlang actor or go goroutine, all meet the characteristics of the association: the programming language implementation and scheduling, user-state switching, the creation of destruction overhead is very small. As for Python, its multi-threaded switching and scheduling is based on the operating system, and because Gil's pit level exists, it is impossible to really do parallel.

And from the author's concurrent programming experience, Erlang's functional programming grammar style and its OTP behavior framework provides an obscure callback (callback) approach that has certain entry thresholds and challenges for most programmers, such as C + + and Java-born programmers. Go, known as the "C in the internet age", has a much better programming experience with its Class C syntax and control flow implementations, as well as the programming paradigm of the object.

Go/python/erlang language Syntax Comparison

All language features require a formal representation, and the detailed comparison of the three language syntaxes of Go, Python, and Erlang is as follows (see the first part of the full big picture, Part Two). Here (link) There is a detailed syntax comparison between go and C, which is one reason why I did not do the go vs. C comparison.

As Rob Pike, one of the designers of the go language, says, "The complexity of software is multiplicative-related." This is fully reflected in the number of language keywords (keyword) control, go keyword is the least, only 25, and Erlang is 27, Python is 31. Fundamentally ensures that the go language is simple and easy to learn.

The go language divides data types into four categories: the underlying type, the composite type, the reference type, and the interface type. The underlying types are: integer, float, complex, String, and Boolean. Composite data types have arrays and structs. Reference types include pointers, slices, dictionaries, functions, and channels. Other data types, such as atom (Atom), bit (binary), tuple (tuple), collection (set), record, go are not supported.

Go has improved many of the grammar features of C, as Rob Pike mentioned in "Less was exponentially more", "Start of Go: C, solve some obvious flaws, remove impurities, add some missing features." ", for example, the Switch/case Case subroutine section default break out, Case statements support numeric range, conditional judgment statement; All types are initialized to 0 by default, no uninitialized variables; the declaration syntax (link) that puts the type behind a variable makes complex declarations more readable , no header files, the compilation of files to package organization, improve the encapsulation capacity, with the empty interface (interface {}) instead of the location of void *, improve the type system capabilities and so on.

Go makes a clear distinction between functions, methods, and interfaces. Like Erlang, go functions as a first citizen. function allows us to package a sequence of statements into a single unit, which can then be called multiple times from other parts of the program. The difference between a function and a method is that there is no receiver, not a return value, as in other languages. The interface type specifically describes a collection of methods, while an empty interface interfac{} indicates that any type can be received. The use of the interface in this 2, with the face of the object programming paradigm analogy, can be analogous to subtype polymorphism (subtype polymorphism) and ad hoc polymorphism (non-parametric polymorphism).

As you can see, the goroutine of Go is a function and a stack allocated to it on the heap. So it's very cheap, we can easily create tens of thousands of goroutine, but they are not scheduled to be executed by the operating system. Goroutine can only use the channel to send to the specified Goroutine request to query for update variables. This is the mantra of Go "don't use shared data to communicate, use communication to share data." Channel supports capacity limits and range iterators.

Lexical contrast of Go/python/erlang language

A detailed comparison of the lexical symbols for Go, Python, and Erlang three languages is shown below (see full larger image). The lexical notation for Go is the largest of the 3 languages, with 41, and more symbols are reused. In contrast, Python is at least 31.

The go language takes a very strong stance in lexical and code formats. The go language has only one means of controlling visibility: uppercase initials are exported from the package that defines them, and lowercase letters do not. This way of restricting the members of a package also applies to a struct or a type of method.

In the file name, go also has certain specifications, such as the _test.go suffix of the source file is a test file, they are part of the Go test test, the test file in the function of the name of the test function is a testing functions, to test some of the program's logical behavior is correct The functions with benchmark as the function name prefix are benchmark functions that measure the performance of some functions.

In addition to the keywords, go also has about 30 predefined names, such as int and true, which correspond primarily to built-in constants, types, and functions.

TDD Go Programming Example

This section develops a Fibonacci algorithm in TDD, showing the features, syntax, and usage of go, such as Go's unit Test technology, concurrent programming, anonymous functions, closures, and so on.

First the unit test file is as follows:

Package Mainimport ("testing") Func Testfib (t *testing. T) {var testdatas = []struct {n    intwant int64}{{0, 0},{1, 1},{2, 1},{3, 2},{4, 3},{16, 987},{32, 2178309},{45, 1134903 170},}for _, Test: = Range Testdatas {n: = test.nwant: = Test.wantgot: = FIB (n) if got! = want {T.errorf ("fib (%d) =%d, want %d\n ", N, got, Want)}}

Recursive-based implementation scenarios:

Func fib1 (n int) Int64 {if n = = 0 | | n = = 1 {return int64 (n)}return fib1 (n-1) + FIB1 (n-2)}

Test results:

[Email protected]$ time Go test
PASS
OK _/home/crbsp/alex/go/fib 9.705s

Real 0m10.045s
User 0m9.968s
SYS 0m0.068s

Concurrency scenarios based on the Goroutine implementation:

Func fib2 (n int) Int64 {var got int64var channel = Make (chan int64, 2) if n = = 0 | | n = = 1 {return int64 (n)}runtime. Gomaxprocs (2) go func () {channel <-fib1 (N-2)} () go func () {channel <-fib1 (N-1)} () got = <-channelgot + = &L T;-channelreturn got}

Test results:

[Email protected]$ time Go test
PASS
OK _/home/crbsp/alex/go/fib 6.118s

Real 0m6.674s
User 0m10.268s
SYS 0m0.148s

Iterative-based implementation scenarios:

Func fib3 (n int) Int64 {var A, b int64a, b = 0, 1for i: = 0; i < n; i++ {A, b = B, A+b}return a}

Test results:

[Email protected]$ time Go test
PASS
OK _/home/crbsp/alex/go/fib 0.002s

Real 0m0.547s
User 0m0.328s
SYS 0m0.172s

Closure-based implementation scenarios:

Func fibWrapper4 () func () Int64 {var A, b int64a, b = 0, 1return func () Int64 {A, b = B, A+breturn a}}func fib4 (n int) int * {var got int64got = 0f: = FibWrapper4 () for I: = 0; i < n; i++ {got = f ()}return got}

Test results:

[Email protected]$ time Go test
PASS
OK _/home/crbsp/alex/go/fib 0.002s

Real 0m0.411s
User 0m0.260s
SYS 0m0.140s

--Finish--

  

  

Go/python/erlang programming language comparison analysis and examples

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.