# 虚心请教，如何看待Julia和numpy的性能对比

1. juliabox上运行 10^7 的数据
2. 本机上运行 10^8 的数据
Python numpy…50.4
Julia built-in…52.6
Julia hand-written simd…53.3
C -ffast-math…60.0
Julia hand-written…114.1
C…117.8
Python built-in…6732.9
Python hand-written…7448.0

numpy的很多函数底层是C的，试想如果numpy没有这个函数，你会怎么办？

1 个赞

1 个赞

numpy和Julia的sum应该产生的机器码相差不大。这个教程想表达的是，在Julia里面你能写类似Python的语法，获得和numpy这样的C+Fortran的库相似的性能。

1 个赞

NumPy的`max`比Julia的`maximum`快不少。

``````julia> using PyCall

julia> @pyimport numpy as np

julia> using BenchmarkTools

julia> using Random; Random.seed!(1); a = rand(10^7); a[rand(1:10^7)] = NaN;

julia> @btime np.max(\$a)
5.631 ms (8 allocations: 368 bytes)
NaN

julia> @btime maximum(\$a)
7.159 ms (0 allocations: 0 bytes)
NaN
``````

``````julia> using ForwardDiff

julia> a = ForwardDiff.Dual.(rand(10^7÷2), rand(10^7÷2));

julia> @btime maximum(\$a)
17.865 ms (0 allocations: 0 bytes)
Dual{Nothing}(0.9999997371954783,0.07221970918741372)
``````

``````julia> using Measurements

julia> a = measurement.(rand(10^7÷2), rand(10^7÷2)); a[1:3]
3-element Array{Measurement{Float64},1}:
0.9741558410075197 ± 0.9935334559578708
0.5791363481531311 ± 0.19502598354585765
0.40606778803723254 ± 0.7092551723515295

julia> @btime maximum(\$a)
43.010 ms (0 allocations: 0 bytes)
0.999999852008173 ± 0.9144925086644928
``````

``````julia> b = ForwardDiff.Dual.(a, a);

julia> @btime maximum(\$b)
1.307 s (64999987 allocations: 2.31 GiB)
Dual{Nothing}(0.999999852008173 ± 0.9144925086644928,0.999999852008173 ± 0.9144925086644928)
``````

6 个赞