Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Julia have an equivalent to R's list.files(recursive = TRUE)?

Tags:

julia

I've written the folloeing function but it is ~50% slower than R's list.files(x, recursive = TRUE). Maybe there is also a way to make it faster.

function list_files(x)
    v = String[]
    for (root, dirs, files) in walkdir(x)
        for file in files 
            file = joinpath(root, file)
            push!(v, file)
        end
    end
    v
end
like image 553
glhrm Avatar asked Nov 29 '25 01:11

glhrm


1 Answers

I dunno, on my machine walkdir seems pretty close to optimal.

julia> using BenchmarkTools

julia> @benchmark list_files($".")
BenchmarkTools.Trial: 195 samples with 1 evaluation.
 Range (min … max):  22.098 ms … 54.305 ms  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     25.395 ms              ┊ GC (median):    0.00%
 Time  (mean ± σ):   25.636 ms ±  3.179 ms  ┊ GC (mean ± σ):  0.13% ± 0.87%

            ▄▂  ▄    ▄ ▅▅  ▅    ▂█                             
  ▆▃██▇▆▆▇█▇██▇▆██▇▇▇█▆██▆▆███▇▅██▅▇▅▁▆▁▅▅▇▅▆▆▁▅▃▁▁▁▃▁▃▁▁▁▁▁▃ ▃
  22.1 ms         Histogram: frequency by time        31.1 ms <

 Memory estimate: 989.60 KiB, allocs estimate: 8987.

julia> @benchmark split(read($`find . -type f`, String), '\n')
BenchmarkTools.Trial: 149 samples with 1 evaluation.
 Range (min … max):  26.661 ms … 47.466 ms  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     32.541 ms              ┊ GC (median):    0.00%
 Time  (mean ± σ):   33.547 ms ±  4.435 ms  ┊ GC (mean ± σ):  0.60% ± 3.90%

            ▄    █    ▂ ▂                                      
  ▃▁▆█▆▅▆█▇▃█▇▇▇▇██████▆█▃█▅▇▁▁▆▅▅▆▁▅▅▃▃▇▅▃▃▅▁▇▁▁▃▃▁▃▁▁▁▃▅▃▃▃ ▃
  26.7 ms         Histogram: frequency by time        45.3 ms <

 Memory estimate: 626.14 KiB, allocs estimate: 79.

If R is really much faster than find . -type f, then that's pretty impressive.

like image 127
BallpointBen Avatar answered Dec 02 '25 04:12

BallpointBen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!