Julia 比 Java 慢得多

编程入门 行业动态 更新时间:2024-10-28 16:26:42
本文介绍了Julia 比 Java 慢得多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是 Julia 新手,我编写了一个简单的函数来计算 RMSE(均方根误差).ratings 是一个评分矩阵,每一行是[user, film, rating].有 1500 万个收视率.rmse() 方法需要 12.0 秒,但 Java 实现大约快 188 倍:0.064 秒.为什么 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 = rating[i,:] 后,它是 0.856 秒(慢 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 = rating[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, use predict(r[i,1], r[i,2]) - r[i,3].
  • square() may be faster than x*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.

更多推荐

Julia 比 Java 慢得多

本文发布于:2023-10-20 21:21:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1512162.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:得多   Julia   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!