JuliaDB の table を csv で保存する方法, おまけの DataFrames との比較

しばらく、julia 1.0 系で使えなかった JuliaDB ですが、ヴァージョンも v0.12.0 になり、julia 1.0 でも使えるようになっていました。

たった、ひとつ不満だった、JuliaDB のテーブルを csv 形式で保存する方法ですが、英語のドキュメントがありました。

julia> t = table(rand(100000),rand(100000),names= [:x,:y],chunks=1);

julia> @time open("text.csv","w") do fid
       println(fid,join(colnames(t),','))
         for i in collect(t)
           println(fid,join(i,','))
         end
       end
  0.198821 seconds (1.60 M allocations: 58.118 MiB, 10.27% gc time)

結構、高速です。

ただ、純粋に書き出しなら、さすがに、DataFrames + CSV も負けてはいないようです。

julia> using DataFrames , CSV

julia> # t = table(rand(100000),rand(100000),names= [:x,:y],chunks=1); # 上述と一緒です

julia> df = DataFrame(x = collect(select(t, :x)), y = collect(select(t, :y)));

julia> @time CSV.write("hoge.csv", df)
  0.099883 seconds (100.08 k allocations: 4.581 MiB)
"hoge.csv"

あまりな大きなデータでない場合は、速度は、Array でも、DataFrame でも JuliaDB.table でも、変らないかもしれません。for ループを Array のまま計算する方法を考える方が良いのかもしれません。JuliaDB は、マクロみたいな書式がデフォルトなので select(t, :x) などと書かなければならない反面、可読性は JuliaBD が良いのかもしれません。とにかく答えが出れば良いだけなら、どのフォーーマットも変らないかもしれません。

読み込みについては、JuliaDB は、1回目が長いです。

% julia
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.1.0 (2019-01-21)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> @time using JuliaDB
  7.468096 seconds (14.07 M allocations: 713.503 MiB, 5.66% gc time)

julia> for i in 1:5; @time loadtable("hoge.csv");end
 12.461837 seconds (30.11 M allocations: 1.451 GiB, 6.09% gc time)
  0.015143 seconds (851 allocations: 2.950 MiB)
  0.017356 seconds (851 allocations: 2.950 MiB)
  0.017306 seconds (851 allocations: 2.950 MiB)
  0.071737 seconds (39.38 k allocations: 4.880 MiB, 11.06% gc time)

julia> @time using CSV, DataFrames
  6.548409 seconds (12.32 M allocations: 629.396 MiB, 5.35% gc time)

julia> for i in 1:5; @time CSV.read("hoge.csv");end
  5.513671 seconds (22.08 M allocations: 882.995 MiB, 10.99% gc time)
  0.053774 seconds (199.26 k allocations: 6.769 MiB)
  0.060115 seconds (199.13 k allocations: 6.761 MiB, 19.13% gc time)
  0.049160 seconds (199.13 k allocations: 6.761 MiB)
  0.050073 seconds (199.13 k allocations: 6.761 MiB)

やはり、慣れの問題もありますので、一般的な使用には DataFrames + CSV なのかもしれません。 読み込みが早ければ、JuliaDB に移行も考えるかもしれません。 julia 0.6 で JuliaDB を使ったときは、2つの表の結合などは DataFrames の方が使い易い場面がありました。改良されて良くなっているなら、JuliaDB も考慮ですね。 少し使ってみないとですが、1つ覚えると他を使う必要はないので、考え所です。

B! LINE