Julia 使用 cat 命令很慢
Julia is slow with cat command
我想看看 julia 语言,所以我写了一个小脚本来导入我正在使用的数据集。但是当我 运行 并分析脚本时,结果发现它比 R 中的类似脚本慢得多。
当我进行分析时,它告诉我所有 cat 命令的性能都很差。
文件如下所示:
#
#Metadata
#
Identifier1 data_string1
Identifier2 data_string2
Identifier3 data_string3
Identifier4 data_string4
//
我主要想获取 data_strings 并将它们拆分为单个字符的矩阵。
这是一个最小的代码示例:
function loadfile()
f = open("/file1")
first=true
m = Array(Any, 1,0)
for ln in eachline(f)
if ln[1] != '#' && ln[1] != '\n' && ln[1] != '/'
s = split(ln[1:end-1])
s = split(s[2],"")
if first
m = reshape(s,1,length(s))
first = false
else
s = reshape(s,1,length(s))
println(size(m))
println(size(s))
m = vcat(m, s)
end
end
end
end
知道为什么 julia 使用 cat 命令可能会很慢,或者我该如何做?
感谢任何建议!
像这样使用 cat
很慢,因为它需要大量内存分配。每次我们执行 vcat
时,我们都会分配一个全新的数组 m
,它与旧的 m
基本相同。这是我如何以更 Julian 的方式重写您的代码,其中 m
仅在最后创建:
function loadfile2()
f = open("./sotest.txt","r")
first = true
lines = Any[]
for ln in eachline(f)
if ln[1] == '#' || ln[1] == '\n' || ln[1] == '/'
continue
end
data_str = split(ln[1:end-1]," ")[2]
data_chars = split(data_str,"")
# Can make even faster (2x in my tests) with
# data_chars = [data_str[i] for i in 1:length(data_str)]
# But this inherently assumes ASCII data
push!(lines, data_chars)
end
m = hcat(lines...)' # Stick column vectors together then transpose
end
我做了一个 10,000 行版本的你的示例数据,发现如下表现:
Old version:
elapsed time: 3.937826405 seconds (3900659448 bytes allocated, 43.81% gc time)
elapsed time: 3.581752309 seconds (3900645648 bytes allocated, 36.02% gc time)
elapsed time: 3.57753696 seconds (3900645648 bytes allocated, 37.52% gc time)
New version:
elapsed time: 0.010351067 seconds (11568448 bytes allocated)
elapsed time: 0.011136188 seconds (11568448 bytes allocated)
elapsed time: 0.010654002 seconds (11568448 bytes allocated)
我想看看 julia 语言,所以我写了一个小脚本来导入我正在使用的数据集。但是当我 运行 并分析脚本时,结果发现它比 R 中的类似脚本慢得多。 当我进行分析时,它告诉我所有 cat 命令的性能都很差。
文件如下所示:
#
#Metadata
#
Identifier1 data_string1
Identifier2 data_string2
Identifier3 data_string3
Identifier4 data_string4
//
我主要想获取 data_strings 并将它们拆分为单个字符的矩阵。 这是一个最小的代码示例:
function loadfile()
f = open("/file1")
first=true
m = Array(Any, 1,0)
for ln in eachline(f)
if ln[1] != '#' && ln[1] != '\n' && ln[1] != '/'
s = split(ln[1:end-1])
s = split(s[2],"")
if first
m = reshape(s,1,length(s))
first = false
else
s = reshape(s,1,length(s))
println(size(m))
println(size(s))
m = vcat(m, s)
end
end
end
end
知道为什么 julia 使用 cat 命令可能会很慢,或者我该如何做?
感谢任何建议!
像这样使用 cat
很慢,因为它需要大量内存分配。每次我们执行 vcat
时,我们都会分配一个全新的数组 m
,它与旧的 m
基本相同。这是我如何以更 Julian 的方式重写您的代码,其中 m
仅在最后创建:
function loadfile2()
f = open("./sotest.txt","r")
first = true
lines = Any[]
for ln in eachline(f)
if ln[1] == '#' || ln[1] == '\n' || ln[1] == '/'
continue
end
data_str = split(ln[1:end-1]," ")[2]
data_chars = split(data_str,"")
# Can make even faster (2x in my tests) with
# data_chars = [data_str[i] for i in 1:length(data_str)]
# But this inherently assumes ASCII data
push!(lines, data_chars)
end
m = hcat(lines...)' # Stick column vectors together then transpose
end
我做了一个 10,000 行版本的你的示例数据,发现如下表现:
Old version:
elapsed time: 3.937826405 seconds (3900659448 bytes allocated, 43.81% gc time)
elapsed time: 3.581752309 seconds (3900645648 bytes allocated, 36.02% gc time)
elapsed time: 3.57753696 seconds (3900645648 bytes allocated, 37.52% gc time)
New version:
elapsed time: 0.010351067 seconds (11568448 bytes allocated)
elapsed time: 0.011136188 seconds (11568448 bytes allocated)
elapsed time: 0.010654002 seconds (11568448 bytes allocated)