changed 9 years ago
Published Linked with GitHub

嘗試使用 go 改善修訂比對效能

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

by Max Wu (jackycute)

@Golang Taipei Gathering #19


HackMD

使用 Markdown 的跨平台即時協作筆記


修訂版本

也就是 Diff



右上角有個「修訂版本」



長這樣


採用 google-diff-match-patch

https://code.google.com/p/google-diff-match-patch/


官方實作語言

Java, JavaScript, Dart, C++, C#, Objective C, Lua, Python


發現什麼了嗎?

對,沒有 Golang


go-diff

https://github.com/sergi/go-diff


GNU diff format (Unified Format)

@@ -3,16 +3,19 @@
 rem
-ipsum dolor
+dolor sit amet
 .

步驟

  • diff_main 比對字串
  • diff_cleanupSemantic 清除多餘的結果
  • patch_make 產生 patch
  • patch_toText 轉成類似 GNU 格式

測試效能 - 短文字

  • 執行 10 次
  • 字串一:Lorem ipsum dolor.
  • 字串二:Lorem dolor sit amet.

Go

  • 平均執行時間:0.063 ms
  • 平均記憶體使用量:4.61 KB

Node.js

  • 平均執行時間:4.7 ms
  • 平均記憶體使用量:131.18 KB

測試結果

  • Go 比 Node.js 快 67 倍
  • Node.js 比 Go 記憶體使用量多 28 倍

你以為這就結束了嗎?


測試效能 - 文字檔

  • 執行 10 次
  • 檔案一:長度 12979 字元
  • 檔案二:長度 11918 字元

Go

  • 平均執行時間:359.783 ms
  • 平均記憶體使用量:1686 KB

Node.js

  • 平均執行時間:1010.0 ms
  • 平均記憶體使用量:2329 KB

測試結果

  • Go 比 Node.js 快 2.8 倍
  • Node.js 比 Go 記憶體使用量多 1.38 倍

結論

  • Go 確實在 修訂比對上比 Node.js 效能好
  • 但是文字長度與效能成反比

謝謝大家
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

https://github.com/jackycute/hackmd-revision-go

Select a repo