朱莉娅比Java慢得多 [英] Julia much slower than Java
问题描述
我是Julia的新手,我编写了一个简单的函数来计算RMSE(均方根误差). ratings
是评分矩阵,每行是[user, film, rating]
.有1500万个收视率. rmse()
方法花费12.0 s,但是Java实现快了188倍:0.064 s.为什么Julia的执行速度这么慢?在Java中,我正在处理Rating
对象的数组,如果它是多维int
数组,则它甚至会更快.
I'm new to Julia and I've written a simple function that calculates RMSE (root mean square error). ratings
is a matrix of ratings, each row is [user, film, rating]
. There are 15 million ratings. The rmse()
method takes 12.0 s, but Java implementation is about 188x faster: 0.064 s. Why is the Julia implementation that slow? In Java, I'm working with an array of Rating
objects, if it was a multidimensional int
array, it would be even faster.
ratings = readdlm("ratings.dat", Int32)
function predict(user, film)
return 3.462
end
function rmse()
total = 0.0
for i in 1:size(ratings, 1)
r = ratings[i,:]
diff = predict(r[1], r[2]) - r[3]
total += diff * diff
end
return sqrt(total / size(ratings)[1])
end
避免了全局变量后,它在1.99秒(比Java慢31倍)中完成.删除r = ratings[i,:]
后,它的速度为0.856 s(慢13倍).
After avoiding the global variable, it finishes in 1.99 s (31x slower than Java). After removing the r = ratings[i,:]
, it's 0.856 s (13x slower).
推荐答案
一些建议:
- 请勿使用全局变量.由于烦人的技术原因,它们运行缓慢.而是将
ratings
作为参数传递. -
r = ratings[i,:]
行进行复制,速度很慢.而是使用predict(r[i,1], r[i,2]) - r[i,3]
. -
square()
可能比x*x
快-试试吧. - 如果您从源头上使用前沿的Julia,请查看全新的
NumericExtensions.jl
软件包,它对许多常见的数值运算都疯狂地优化了功能. (请参阅julia-dev列表) - Julia必须在第一次执行代码时对其进行编译.在Julia中进行基准测试的正确方法是多次计时,而忽略第一次.
- Don't use globals. For annoying technical reasons, they're slow. Instead, pass
ratings
in as an argument. - The
r = ratings[i,:]
line makes a copy, which is slow. Instead, usepredict(r[i,1], r[i,2]) - r[i,3]
. square()
may be faster thanx*x
-- try it.- If you're using the bleeding-edge Julia from source, check out the brand new
NumericExtensions.jl
package, which has insanely optimized functions for many common numerical operations. (see the julia-dev list) - Julia has to compile the code the first time it executes it. The right way to benchmark in Julia is to do the timing several times and ignore the first time through.
这篇关于朱莉娅比Java慢得多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!