小编cno*_*cno的帖子

Julia:函数类型和性能

Julia 有没有办法概括如下所示的模式?

function compute_sum(xs::Vector{Float64})
    res = 0
    for i in 1:length(xs)
        res += sqrt(xs[i])
    end
    res
end
Run Code Online (Sandbox Code Playgroud)

这将计算每个向量元素的平方根,然后对所有内容求和。它比具有数组理解或的“天真”版本快得多map,并且也不分配额外的内存:

xs = rand(1000)

julia> @time compute_sum(xs)
  0.000004 seconds
676.8372556762225

julia> @time sum([sqrt(x) for x in xs])
  0.000013 seconds (3 allocations: 7.969 KiB)
676.837255676223

julia> @time sum(map(sqrt, xs))
  0.000013 seconds (3 allocations: 7.969 KiB)
676.837255676223
Run Code Online (Sandbox Code Playgroud)

不幸的是,“明显的”通用版本的性能很差:

function compute_sum2(xs::Vector{Float64}, fn::Function)
    res = 0
    for i in 1:length(xs)
        res += fn(xs[i])
    end
    res
end

julia> @time compute_sum2(xs, x -> sqrt(x))
  0.013537 seconds (19.34 …
Run Code Online (Sandbox Code Playgroud)

performance julia

9
推荐指数
2
解决办法
142
查看次数

Julia:CSV.write 内存效率很低?

我注意到,当将大型数据帧保存为 CSV 时,内存分配比内存中数据帧的大小(或磁盘上 CSV 文件的大小)高一个数量级,至少高出 10 倍。 为什么这是案件?有没有办法防止这种情况?即有没有一种方法可以将数据帧保存到磁盘而不使用比实际数据帧更多的内存?

在下面的示例中,我生成了一个包含一个整数列和 10m 行的数据框。它重 76MB,但写入 CSV 分配了 1.35GB。

using DataFrames, CSV

function generate_df(n::Int64)
    DataFrame!(a = 1:n)
end
Run Code Online (Sandbox Code Playgroud)
julia> @time tmp = generate_df2(10000000);
  0.671053 seconds (2.45 M allocations: 199.961 MiB)

julia> Base.summarysize(tmp) / 1024 / 1024
76.29454803466797

julia> @time CSV.write("~/tmp/test.csv", tmp)
  3.199506 seconds (60.11 M allocations: 1.351 GiB)
Run Code Online (Sandbox Code Playgroud)

dataframe julia

5
推荐指数
1
解决办法
202
查看次数

Julia:内存中存在大量可变结构时,为什么 GC 会很慢

与非可变结构相比,为什么当大量可变结构加载到内存中时,垃圾回收速度要慢得多?在这两种情况下,对象树应该具有相同的大小。

julia> struct Foo
           a::Float64
           b::Float64
           c::Float64
       end

julia> mutable struct Bar
           a::Float64
           b::Float64
           c::Float64
       end

julia> @time dat1 = [Foo(0.0, 0.0, 0.0) for i in 1:1e9];
  9.706709 seconds (371.88 k allocations: 22.371 GiB, 0.14% gc time)

julia> @time GC.gc(true)
  0.104186 seconds, 100.00% gc time

julia> @time GC.gc(true)
  0.124675 seconds, 100.00% gc time

julia> @time dat2 = [Bar(0.0, 0.0, 0.0) for i in 1:1e9];
 71.870870 seconds (1.00 G allocations: 37.256 GiB, 73.88% gc time)

julia> @time GC.gc(true)
 47.695473 seconds, 100.00% …
Run Code Online (Sandbox Code Playgroud)

julia

3
推荐指数
1
解决办法
523
查看次数

串扰:在Leaflet中过滤折线

我无法让串扰与传单和折线一起工作 - 这是一个 MWE:

library(crosstalk)
library(leaflet)

theta <- seq(0, 2*pi, len = 100)
dat <- data.frame(
  lon = cos(theta),
  lat = sin(theta),
  t = 1:100
)

sd <- SharedData$new(dat)

map <- leaflet() %>%
  addTiles() %>%
  addCircleMarkers(data = sd, lat = ~lat, lng = ~lon, color = "blue") %>%
  addPolylines(data = sd, lat = ~lat, lng = ~lon, color = "blue")

bscols(
  filter_slider("t", "Time", sd, column = ~t),
  map
)
Run Code Online (Sandbox Code Playgroud)

时间 filter_slider 适用于圆形标记,但不适用于折线。

如果有人能给我指出正确的方向,我很乐意在 R 传单包中尝试修复此问题。即需要改变/实施什么?我认为目前 javascript 方面缺少支持?

r rstudio leaflet

2
推荐指数
1
解决办法
3505
查看次数

标签 统计

julia ×3

dataframe ×1

leaflet ×1

performance ×1

r ×1

rstudio ×1