Julia 比较两个缓冲区是否相等

Julia 比较两个缓冲区是否相等,julia,Julia,有没有办法直接比较两个缓冲区? 例如,有两个相同的文件file1和file1 copy,我希望执行以下操作: f1 = open(file1) f2 = open(file1-copy) if f1 == f2 println("Equal content") end 我知道我可以把这些串起来比较: if readstring(f1) == readstring(f2) println("Equal content") end 我猜不是直接比较两个缓冲区SF1和f2,但是如

有没有办法直接比较两个缓冲区? 例如,有两个相同的文件file1和file1 copy,我希望执行以下操作:

f1 = open(file1)
f2 = open(file1-copy)
if f1 == f2
     println("Equal content")
end
我知道我可以把这些串起来比较:

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

我猜不是直接比较两个缓冲区SF1和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

我猜不是直接比较两个缓冲区SF1和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

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


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


使用Mmap.Mmap和readstring有什么区别?似乎他们都使用memcmp进行比较。我知道Mmap.Mmap适用于大文件,很难说哪个更容易;readstring将整个文件复制到内存中,然后对它们进行比较,如果不需要的话,这可能会很昂贵。mmap将其保留在磁盘上,但允许您将其视为内存阵列。如果确实存在任何差异,这也允许它更快地短路-它不需要读取整个文件。感谢您的解释!现在这是有道理的。我可能误解了这个问题:考虑到readstring示例,我认为作者正在寻找一种方法来验证两个相同的文件,而不显式比较内容。我的实际问题有点困难。我有一个文件和一个PipeBuffer,我想比较它们。我漏掉了这个信息,因为它几乎是一样的。事实并非如此,PipeBuffer的数据已经在内存中使用Mmap.Mmap和readstring有什么区别?似乎他们都使用memcmp进行比较。我知道Mmap.Mmap适用于大文件,很难说哪个更容易;readstring将整个文件复制到内存中,然后对它们进行比较,如果不需要的话,这可能会很昂贵。mmap将其保留在磁盘上,但允许您将其视为内存阵列。如果确实存在任何差异,这也允许它更快地短路-它不需要读取整个文件。感谢您的解释!现在这是有道理的。我可能误解了这个问题:考虑到readstring示例,我认为作者正在寻找一种方法来验证两个相同的文件,而不显式比较内容。我的实际问题有点困难。我有一个文件和一个PipeBuffer,我想比较它们。我漏掉了这个信息,因为它几乎是一样的。事实并非如此,PipeBuffer的数据已经在内存中接受的答案是使用Mmap.Mmap,因为它减少了从磁盘到内存的复制操作,从而解决了这里描述的问题。但是,如果缓冲区的数据已经在内存中,只比较散列更明智。公认的答案是使用Mmap.Mmap,因为它减少了从磁盘到内存的复制操作,从而解决了此处描述的问题。但是,如果缓冲区的数据已经在内存中,只比较散列更明智。