pinyin
pinyin copied to clipboard
算法优化尝试
@TooBug 有很好的一个 PR #26 ,这个 PR 其实早已经无法合并进来了,但是我一直没舍得关掉,因为有很好的参考价值。
- 二进制字典。
- 二分法查找。
我计划近期也尝试更新使用这两者,另外其他的一些技术(比如偏移量)也会进行尝试。
如果尝试的结果满足甚至超出预期,会合并且升级版本到 3.0.x
目前 @TooBug/pinyin 是每个汉字转换都会 fs.openSync 二进制字典,我计划只打开一次,必要的话直接装载到内存(不知道和现在直接装载整个 JSON 字典差异有多大)。
谢谢 @TooBug,并 Close #26.
现有数据对比:
short(time) | short(memory) | long(time) | long(memory) | |
---|---|---|---|---|
@TooBug/[email protected] | 13ms | 2052096 (1.96M) | 344ms | 10543104 (10.05M) |
@hotoo/[email protected] | 109ms | 30367744 (28.96M) | 105ms | 31322112 (29.87M) |
@TooBug/[email protected] (only calc) | 5ms | 2166784 (2.07M) | 321ms | 10887168 (10.38M) |
@hotoo/[email protected] (only calc) | 1ms | 30130176 (28.73M) | 6ms | 31363072 (29.91M) |
@TooBug/[email protected] (twice long, only calc) | 625ms | 10817536 (10.32M) | ||
@hotoo/[email protected] (twice long, only calc) | 7ms | 31989760 (30.51M) | ||
@TooBug/[email protected] (tenfold long, only calc) | 2984ms | 27246592 (25.98M) | ||
@hotoo/[email protected] (tenfold long, only calc) | 21ms | 33804288 (32.24M) | ||
@TooBug/[email protected] (twenty times long, only calc) | 5981ms | 43524096 (41.51M) | ||
@hotoo/[email protected] (twenty times long, only calc) | 38ms | 37453824 (35.72.24M) |

从现有的数据来看,TooBug 的版本除了初始化 pinyin 模块性能较高之外,其他还是可圈可点(当然 TooBug 的版本还有些可优化的地方)。
Refenence
- How to Use Buffers in Node.js - docs.nodejitsu.com
- Buffer io.js v2.5.0 Manual & Documentation
- 用 JavaScript 处理 binary data
- Is there a way to read binary data in JavaScript? - Stack Overflow
- node.js - Node Buffers, from utf8 to binary - Stack Overflow
- Reading and Writing Binary Files Using JScript - CodeProject
- substack/node-binary
- substack/node-bufferlist
- 用 JavaScript 读写二进制文件
- 用 JavaScript 读写二进制文件的另一种方法
- 文本文件与二进制文件区别 - kunzj - 博客园
- ArrayBuffer
Yeah,加油!3.0性能表现好的话再用回来,减少生态中的重复模块!
[email protected] 开始默认关闭汉语分词,所以除了使用内存较多(但比较稳定,30M 左右,开启分词另算),一旦模块加载好,计算转换成拼音的速度还是比 @TooBug/pinyin 快很多的。
而且 @TooBug/pinyin 的内存使用和计算转换耗时随着字符串的增长而增长的非常快(@hotoo/pinyin 会比较稳定),看上面数据。
如果你不开启分词,并且预装载好 pinyin 模块,现在就可以使用 @hotoo/pinyin 了。
3.0 主要考虑在保持计算性能没有明显下降的情况下,尽量减少内存使用。
https://github.com/breeswish/pinyinlite
前端项目需要,写了一个只做输出不做内容判断、词库比较大的拼音库。其中用到的优化,在内存和时间上都取得了很好的效果,可以考虑 port 一下 :-)
@SummerWish 好的,我抽空研究下你的作品。