为什么我的 Julia 代码运行速度比 javascript 慢?
Why my Julia code runs slower than javascript?
最近,我对 Julia-lang 很感兴趣,因为它声称是一种具有接近 C 性能的动态语言。但是,到目前为止,我对它的体验并不好(至少在性能方面)。
我正在编写的应用程序需要随机访问特定的数组索引,然后将它们的值与其他特定的数组索引进行比较(经过多次迭代)。下面的代码从程序中模拟了我的需求:
我的 Julia 代码在大约 8 秒内完成执行,而 java-脚本代码在 chrome 环境中需要不到 1 秒!
我在 Julia 代码上做错了什么吗?非常感谢。
Julia 代码在这里:
n=5000;
x=rand(n)
y=rand(n)
mn=ones(n)*1000;
tic();
for i in 1:n;
for j in 1:n;
c=abs(x[j]-y[i]);
if(c<mn[i])
mn[i]=c;
end
end
end
toc();
Javascript 代码:(比上面的 julia 代码快 8 倍以上!)
n=5000; x=[]; y=[]; mn=[];
for(var i=0; i<n; i++){x.push(Math.random(1))}
for(var i=0; i<n; i++){y.push(Math.random(1))}
for(var i=0; i<n; i++){mn.push(1000)}
console.time('test');
for(var i=0; i<n; i++){
for(var j=0; j<n; j++){
c=Math.abs(x[j]-y[i]);
if(c<mn[i]){
mn[i]=c;
}
}
}
console.timeEnd('test');
Julia 代码应始终位于函数内部,以便编译器对其进行优化。此外,您同时测量了编译时间和执行时间:要获得准确的测量结果,您应该调用该函数两次(第一次用于编译)。
function test(n)
x=rand(n);
y=rand(n);
mn=ones(n)*1000;
for i in 1:n;
for j in 1:n;
c=abs(x[j]-y[i])
if(c<mn[i])
mn[i]=c
end
end
end
(mn, x, y)
end
test(1)
@time test(5000);
这在我的笔记本电脑上花费了 0.04 秒。 javascript 铬 1s
(javascript 在 firefox 网络控制台 53s 中)
Performance Tips
Avoid global variables
A global variable might have its value, and therefore its type, change
at any point. This makes it difficult for the compiler to optimize
code using global variables. Variables should be local, or passed as
arguments to functions, whenever possible.
Any code that is performance critical or being benchmarked should be
inside a function.
We find that global names are frequently constants, and declaring them
as such greatly improves performance:
julia> const n = 5000; const x, y = rand(n), rand(n); const mn = fill(1000.0, n);
julia> function foo!(mn, x, y)
n = length(mn)
@inbounds for i in 1:n, j in 1:n
c = abs(x[j] - y[i])
if(c < mn[i])
mn[i] = c
end
end
return mn
end
foo! (generic function with 1 method)
julia> using BenchmarkTools: @btime
julia> @btime foo!(mn, x, y)
15.432 ms (0 allocations: 0 bytes)
最近,我对 Julia-lang 很感兴趣,因为它声称是一种具有接近 C 性能的动态语言。但是,到目前为止,我对它的体验并不好(至少在性能方面)。 我正在编写的应用程序需要随机访问特定的数组索引,然后将它们的值与其他特定的数组索引进行比较(经过多次迭代)。下面的代码从程序中模拟了我的需求: 我的 Julia 代码在大约 8 秒内完成执行,而 java-脚本代码在 chrome 环境中需要不到 1 秒! 我在 Julia 代码上做错了什么吗?非常感谢。
Julia 代码在这里:
n=5000;
x=rand(n)
y=rand(n)
mn=ones(n)*1000;
tic();
for i in 1:n;
for j in 1:n;
c=abs(x[j]-y[i]);
if(c<mn[i])
mn[i]=c;
end
end
end
toc();
Javascript 代码:(比上面的 julia 代码快 8 倍以上!)
n=5000; x=[]; y=[]; mn=[];
for(var i=0; i<n; i++){x.push(Math.random(1))}
for(var i=0; i<n; i++){y.push(Math.random(1))}
for(var i=0; i<n; i++){mn.push(1000)}
console.time('test');
for(var i=0; i<n; i++){
for(var j=0; j<n; j++){
c=Math.abs(x[j]-y[i]);
if(c<mn[i]){
mn[i]=c;
}
}
}
console.timeEnd('test');
Julia 代码应始终位于函数内部,以便编译器对其进行优化。此外,您同时测量了编译时间和执行时间:要获得准确的测量结果,您应该调用该函数两次(第一次用于编译)。
function test(n)
x=rand(n);
y=rand(n);
mn=ones(n)*1000;
for i in 1:n;
for j in 1:n;
c=abs(x[j]-y[i])
if(c<mn[i])
mn[i]=c
end
end
end
(mn, x, y)
end
test(1)
@time test(5000);
这在我的笔记本电脑上花费了 0.04 秒。 javascript 铬 1s (javascript 在 firefox 网络控制台 53s 中)
Performance Tips
Avoid global variables
A global variable might have its value, and therefore its type, change at any point. This makes it difficult for the compiler to optimize code using global variables. Variables should be local, or passed as arguments to functions, whenever possible.
Any code that is performance critical or being benchmarked should be inside a function.
We find that global names are frequently constants, and declaring them as such greatly improves performance:
julia> const n = 5000; const x, y = rand(n), rand(n); const mn = fill(1000.0, n);
julia> function foo!(mn, x, y)
n = length(mn)
@inbounds for i in 1:n, j in 1:n
c = abs(x[j] - y[i])
if(c < mn[i])
mn[i] = c
end
end
return mn
end
foo! (generic function with 1 method)
julia> using BenchmarkTools: @btime
julia> @btime foo!(mn, x, y)
15.432 ms (0 allocations: 0 bytes)