比较两个缓冲区是否相等

时间:2018-03-20 11:36:47

标签: julia

有没有办法直接比较两个缓冲区? 例如,有两个相同的文件file1file1-copy,我想这样做:

f1 = open(file1)
f2 = open(file1-copy)
if f1 == f2
     println("Equal content")
end

我知道我可以制作相应的字符串并进行比较:

if readstring(f1) == readstring(f2)
    println("Equal content")
end

2 个答案:

答案 0 :(得分:5)

最简单的方法可能就是mmap他们:

julia> f1 = open("file")
       f2 = open("file-copy");

julia> Mmap.mmap(f1) == Mmap.mmap(f2)
true

答案 1 :(得分:1)

我猜不是(在某种意义上比较两个缓冲区(f1& f2直接),但是如果你可以预先计算它们的哈希值,它会方便以后直接比较它们:

julia> using SHA

shell> cat file1
Is there a way to compare two buffers directly? For example having two identical files file1 and file1-copy, i would like to do:

f1 = open(file1)
f2 = open(file1-copy)
if f1 == f2
     println("Equal content")
end

I know i can make strings of that and compare those:

if readstring(f1) == readstring(f2)
    println("Equal content")
end

julia> file1 = open("file1") do f
           sha256(f)
       end |> bytes2hex
"eb179202793cfbfd1a1f19e441e813a8e23012a5bdd81e453daa266fcb74144a"

julia> file1copy = open("file1-copy") do f
           sha256(f)
       end |> bytes2hex
"eb179202793cfbfd1a1f19e441e813a8e23012a5bdd81e453daa266fcb74144a"

julia> file1 == file1copy
true