2015-09-06 08:40
浏览 587


I want to know the contents when file changed

there is a simeple txt file

textOne 1,2,3,4,5,6,7,8,9,10

and i change it to

textTwo 1,2,3,4,7,7,7,8,9,10

so 'One' and '5,6' changed to 'two', '7,7'

I can find things which changed by loop. but I wonder if there was some better way to check

图片转代码服务由CSDN问答提供 功能建议


有一个半 txt文件

  textOne 1,2,3,4,5,6,7,8,9,10 


  textTwo 1,2,3,4,7,7,7,8,9,10 


我可以找到循环改变的事物。 但我想知道是否有更好的方法来检查

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douliao8318 2015-09-06 10:12

    Instead of looping use standard third-party library, with small optimisation.

    One of the library that I've used in past is Go port of google-diff-match-patch (same library that @Not_a_golfer suggested in the comment.).

    You can optimise this by first calculating sha2 hash of two files, and if they are not the same, you can assume that they are changed, otherwise (probably) they are same, and skip the diff operation.

    One drawback of this optimisation is that, because of pigeon-hole principle, it is possible theoretically to have same hash value, for different contents. But, the probability of happening that is quite small.

    EDIT (based on @elithrar's comment): Since calculation of hash for very large file, can be time consuming. You can calculate sha2 in chunks (size depends on particular hash algorithm from sha2 family). This enables you to bail out early, and improves speed.

    解决 无用
    打赏 举报

相关推荐 更多相似问题