比较两个缓冲区是否相等

hr0*_*r0m 1 julia

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

f1 = open(file1)
f2 = open(file1-copy)
if f1 == f2
     println("Equal content")
end
Run Code Online (Sandbox Code Playgroud)

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

if readstring(f1) == readstring(f2)
    println("Equal content")
end
Run Code Online (Sandbox Code Playgroud)

Mat*_* B. 5

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

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

julia> Mmap.mmap(f1) == Mmap.mmap(f2)
true
Run Code Online (Sandbox Code Playgroud)

  • `readstring`将整个文件复制到内存中,然后比较它们,如果你不需要它,这可能会很昂贵.`mmap`将它留在磁盘上但允许你将其视为内存中的数组.如果确实存在任何差异,这也允许它更快地短路 - 它不需要读取整个文件. (2认同)