Skip to content

Example runs slowly #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Beliavsky opened this issue Jun 5, 2021 · 12 comments · Fixed by #68
Closed

Example runs slowly #47

Beliavsky opened this issue Jun 5, 2021 · 12 comments · Fixed by #68

Comments

@Beliavsky
Copy link

Downloading the package and running the example script (saved as xloess.jl) in the Readme.md on Windows 10 with Julia 1.6.1, using julia xloess.jl takes 36 s of wall time. This is a long time for a small data set of 100 points. A similar code in R or Fortran runs instantly.

@pnavaro
Copy link

pnavaro commented Jun 5, 2021

Yes, it is true, it is due to the compilation time. With Julia you need to adapt your workflow. You can't use it like R or Fortran.

julia> @time include("xloess.jl")
 58.854341 seconds (100.47 M allocations: 6.107 GiB, 4.95% gc time, 0.01% compilation time)
false

julia> @time include("xloess.jl")
  0.729143 seconds (1.10 M allocations: 57.652 MiB, 3.88% gc time, 59.25% compilation time)
false

The second time you include the program, it runs instantly.

@lmiq
Copy link

lmiq commented Jun 6, 2021

Probably worth mentioning that what takes time there is the loading of the Gadfly plotting library. Commenting the plotting part we get:

julia> @time include("./test.jl")
2.449248 seconds (6.63 M allocations: 379.517 MiB, 3.65% gc time, 0.21% compilation time)

and for the second run:

julia> @time include("./runtests.jl")
0.003383 seconds (14.14 k allocations: 3.003 MiB)

@Beliavsky
Copy link
Author

Thanks @lmiq. Is there a Julia plotting library that loads faster than Gadfly?

@lmiq
Copy link

lmiq commented Jun 6, 2021

Gadfly seems particularly slow. Using Plots with the standard backend (GR), takes 10s here compared to 24s of Gadfly, and using GR directly takes ~5s.

time julia -e "using Gadfly; p=plot(rand(10)); draw(SVG("test.svg", 6inch, 3inch), p)"
real 0m24,984s

time julia -e "using Plots; plot(rand(10)); savefig("test.svg")"
real 0m10,216s

time julia -e "using GR; plot(rand(10)); savefig("test.svg")"
real 0m5,529s

But in any case, if the goal is to use Julia as a script (and speed is necessary), probably something like https://github.com/dmolina/DaemonMode.jl is interesting.

@ayushpatnaikgit
Copy link

ayushpatnaikgit commented Sep 7, 2021

Hi,
I am switching my code from R to Julia (primarily for speed). I have translated most of my code and in some places, I've used RCall. My code uses RCall for Loess, but I am hoping to switch to native Julia using Loess.jl. When I try the example (without Gadly), it seems RCalling loess is much faster than Loess.jl and doesn't use even a fraction of the memory. Here is my simple code for benchmarking:

function LoessCall() # Function to call the example 
    xs = 10 .* rand(100)
    ys = sin.(xs) .+ 0.5 * rand(100)
    model = loess(xs, ys)
    return predict(model, xs)
end

LoessCall() # Calling it to compiled it. 


using RCall

R"""
RLoess <- function(x, y) 
{
    m.loess      <- loess(y ~ x)
    return <- predict(m.loess, x)
}
"""

function RLoessCall() # Function to call the example in R
    xs = 10 .* rand(100)
    ys = sin.(xs) .+ 0.5 * rand(100)
    return rcall(:RLoess, xs, ys)
end

RLoessCall() # Calling it to compiled it. 
@time for i in 1:10000 
    RLoessCall()
 end
9.363060 seconds (310.00 k allocations: 49.744 MiB, 0.08% gc time)
@time for i in 1:10000 
    LoessCall()
end
 12.357978 seconds (101.69 M allocations: 27.990 GiB, 19.11% gc time)

@pnavaro
Copy link

pnavaro commented Sep 7, 2021

Dear Ayush

The loess function in R is written in C language and compiled so it is not surprising . Performance is nice but you can choose Julia for other reason like multiple dispatch and the syntax. There is a small mistake in your R code

RLoess <- function(x, y) 
{
    m.loess      <- loess(y ~ x)
    return(predict(m.loess, x))
}

@ayushpatnaikgit
Copy link

Hi @pnavaro

I forgot to mention that I did run LoessCall() to compile it. I will edit my comment and mention it.

Thanks for pointing out the correction in the R code. It's a different style, but both commands work (at least in R 3.6 upwards).

@pnavaro
Copy link

pnavaro commented Sep 7, 2021

The main error was here loess(x ~ y) . I think you mean loess(y ~ x)

@ayushpatnaikgit
Copy link

ayushpatnaikgit commented Sep 7, 2021

Oh.. yes, you are right. I will edit the comment. It doesn't change the time though.

@nalimilan
Copy link
Member

AFAICT none of the performance issues mentioned here are related to Loess.jl. They are rather due to the plotting. So I'm closing this, feel free to reopen if you see slowdowns without calling using Plots.

@nalimilan
Copy link
Member

Ah sorry I missed #47 (comment). Reopening then, though it would be clearer to file a separate issue as it's completely distinct from the original description.

@nalimilan nalimilan reopened this Jul 2, 2022
@ayushpatnaikgit
Copy link

@carljv you've mentioned a trick to speed up loess: http://slendermeans.org/lowess-speed.html

I think the same is applicable here as well. What's your opinion?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants