Julia是一款为技术计算(technical computing)而设计的编程语言。它的语法和其它科学计算语言的语法也有很多相似之处,简单易学。Julia提供了一个复杂精巧的编译器,分布式的并行计算环境,精确的数值计算,以及一个可扩展的数学函数库。 Julia的基本库在线性代数,随机数产生器,信号处理和字符串处理方面引入了很多在单项表现极佳,成熟的开源C/Fortran库之外,大部分是使用Julia语言本身完成的。特别地,Julia的开发者社区正在快速地通过Julia内建的包管理器添加着各种外部扩展包。

Julia的程序代码主要通过围绕多重派发的编程范式,通过对不同的类型组合(类型是用户可以自由定义的)定义和重载函数来进行组织。对于在不同系统上使用Julia的好处的讨论可以在下面的部分或者是在线指南介绍中找到。


Julia语言的特性

  • 多重派发:提供为多种类型定义函数的能力
  • 动态的类型系统
  • 高性能,接近静态编译语言,如 C 语言
  • 内置的包管理器
  • 像Lisp一样真正的宏以及其它元编程特性
  • 可以自由地通过PyCall调用Python程序
  • 像 Shell 一样强大的管理其他进程的能力
  • 为分布式和并行计算所设计
  • 轻量级“绿色”线程(协程
  • 用户定义的类型和内建类型一样快
  • Automatic generation of efficient, specialized code for different argument types
  • 美观可扩展的类型转换与提升
  • 高效支持 Unicode, 包括且不只 UTF-8
  • 免费开源(MIT 协议

高性能的JIT编译器

通过使用类型推断和 即时(JIT)编译 ,以及 LLVM , Julia 具有可选的类型声明,重载,高性能等特性。为了能让您对Julia的性能和其它语言在数值计算和科学计算中有一个更好的比较,我们为多种不同语言写了一些简单的标准测试程序:

FortranJuliaPythonRMatlabOctaveMathe-maticaJavaScriptGoLuaJITJava
gcc 5.1.1 0.4.0 3.4.3 3.2.2 R2015b 4.0.0 10.2.0 V8 3.28.71.19 go1.5 gsl-shell 2.3.1 1.8.0_45
fib0.702.1177.76533.5226.899324.35118.533.361.861.711.21
parse_int5.051.4517.0245.73802.529581.4415.026.061.205.773.35
quicksort1.311.1532.89264.544.921866.0143.232.701.292.032.60
mandel0.810.7915.3253.167.58451.815.130.661.110.671.35
pi_sum1.001.0021.999.561.00299.311.691.011.001.001.00
rand_mat_stat1.451.6617.9314.5614.5230.935.952.302.963.273.92
rand_mat_mul3.481.021.141.571.121.121.3015.071.421.162.36

Figure: 基准测试时间是相对于C 语言的(时间越短越好, C 的用时为单位1).

C 和 Fortran 语言使用了 gcc 5.11 编译, 使用最佳的优化 (从-O0到 -O3)。 C,Fortran,Go和Julia都使用了OpenBLAS v0.2.14.Python 3 通过 Anaconda distribution 安装。rand_mat_stat 和 rand_mat_mul 算法在Python中通过numpy(v1.9.2)中的函数实现,其它部分均为纯Python语言实现。

function mandel(z)
    c = z
    maxiter = 80
    for n = 1:maxiter
        if abs(z) > 2
            return n-1
        end
        z = z^2 + c
    end
    return maxiter
end

function randmatstat(t)
    n = 5
    v = zeros(t)
    w = zeros(t)
    for i = 1:t
        a = randn(n,n)
        b = randn(n,n)
        c = randn(n,n)
        d = randn(n,n)
        P = [a b c d]
        Q = [a b; c d]
        v[i] = trace((P.'*P)^4)
        w[i] = trace((Q.'*Q)^4)
    end
    std(v)/mean(v), std(w)/mean(w)
end

上面的代码是很简洁,并且对于任何一个使用过其它编程语言的人来说都很熟悉。Julia对randmatstat的实现也是相较于同样的C语言实现极其简单的, 并且没有很多性能上的牺牲。计划中的编译器优化将会在未来弥补这一点点在性能上的不足(相较于C语言)。通过语言本身的设计,Julia将使得你能够仅以牺牲少量的性能就可以编写底层的循环,到高级的编程风格应用,用简单的方式实现复杂的算法。 This continuous spectrum of programming levels is a hallmark of the Julia approach to programming and is very much an intentional feature of the language.

为分布式与云计算设计

Julia并没有给

Julia does not impose any particular style of parallelism on the user. Instead, it provides a number of key building blocks for distributed computation, making it flexible enough to support a number of styles of parallelism, and allowing users to add more. The following simple example demonstrates how to count the number of heads in a large number of coin tosses in parallel.

nheads = @parallel (+) for i=1:100000000
  int(randbool())
end

This computation is automatically distributed across all available compute nodes, and the result, reduced by summation (+), is returned at the calling node.

Here is a screenshot of a web-based interactive IJulia Notebook session, using Gadfly. JuliaBox provides a way to run IJulia notebooks in your browser on Docker sandboxed containers provisioned on demand.

This paves the way for fully cloud-based operation, including data management, code editing and sharing, execution, debugging, collaboration, analysis, data exploration, and visualization. The eventual goal is to let people stop worrying about administering machines and managing data and get straight to the real problem.

Gadfly can produce various plots with various rendering backends in the browser (SVG, PDF, PNG and various other backends are also supported). Interactivity can be added to graphs and plots with the Interact.jl package. A small sampling of the capabilities of Gadfly is presented below.

Free, Open Source and Library-Friendly

The core of the Julia implementation is licensed under the MIT license. Various libraries used by the Julia environment include their own licenses such as the GPL, LGPL, and BSD (therefore the environment, which consists of the language, user interfaces, and libraries, is under the GPL). The language can be built as a shared library, so users can combine Julia with their own C/Fortran code or proprietary third-party libraries. Furthermore, Julia makes it simple to call external functions in C and Fortran shared libraries, without writing any wrapper code or even recompiling existing code. You can try calling external library functions directly from Julia’s interactive prompt, getting immediate feedback. See LICENSE for the full terms of Julia’s licensing.