Skip to content

Commit

Permalink
perf: improve performance by reduce memory allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed May 24, 2023
1 parent 444690e commit 23f7307
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 63 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
authors = ["LongYinan <[email protected]>"]
edition = "2018"
edition = "2021"
name = "napi-pinyin"
version = "0.1.0"

Expand All @@ -27,3 +27,4 @@ napi-build = "2"
[profile.release]
lto = true
opt-level = 3
codegen-units = 1
45 changes: 33 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,41 +45,63 @@ yarn add @napi-rs/pinyin

## [pinyin](https://github.com/hotoo/pinyin) 性能对比

Benchmark over `pinyin` package:
Benchmark over [`pinyin`](https://github.com/hotoo/pinyin) and [`pinyin-pro`](https://github.com/zh-lx/pinyin-pro) package:

> **Note**
>
> [`pinyin-pro`](https://github.com/zh-lx/pinyin-pro) doesn't support segment feature.
System info

```
OS: macOS 12.3.1 21E258 arm64
Host: MacBookPro18,2
Kernel: 21.4.0
Shell: zsh 5.8
CPU: Apple M1 Max
GPU: Apple M1 Max
Memory: 9539MiB / 65536MiB
```

```bash
Running "Short input without segment" suite...
Progress: 100%

@napi-rs/pinyin:
962 035 ops/s, ±0.68% | fastest
2 168 325 ops/s, ±0.36% | fastest

pinyin-pro:
1 595 118 ops/s, ±0.18% | 26.44% slower

node-pinyin:
434 241 ops/s, ±0.66% | slowest, 54.86% slower
522 675 ops/s, ±0.09% | slowest, 75.89% slower

Finished 2 cases!
Finished 3 cases!
Fastest: @napi-rs/pinyin
Slowest: node-pinyin
Running "Long input without segment" suite...
Progress: 100%

@napi-rs/pinyin:
59 ops/s, ±0.83% | fastest
566 ops/s, ±0.27% | fastest

pinyin-pro:
215 ops/s, ±0.60% | 62.01% slower

node-pinyin:
2 ops/s, ±3.30% | slowest, 96.61% slower
1 ops/s, ±3.09% | slowest, 99.82% slower

Finished 2 cases!
Finished 3 cases!
Fastest: @napi-rs/pinyin
Slowest: node-pinyin
Running "Short input with segment" suite...
Progress: 100%

@napi-rs/pinyin:
530 228 ops/s, ±1.94% | fastest
885 238 ops/s, ±2.00% | fastest

node-pinyin:
307 788 ops/s, ±0.83% | slowest, 41.95% slower
475 546 ops/s, ±0.42% | slowest, 46.28% slower

Finished 2 cases!
Fastest: @napi-rs/pinyin
Expand All @@ -88,15 +110,14 @@ Running "Long input with segment" suite...
Progress: 100%

@napi-rs/pinyin:
152 ops/s, ±1.09% | fastest
317 ops/s, ±0.46% | fastest

node-pinyin:
3 ops/s, ±3.08% | slowest, 98.03% slower
5 ops/s, ±3.66% | slowest, 98.42% slower

Finished 2 cases!
Fastest: @napi-rs/pinyin
Slowest: node-pinyin
✨ Done in 53.36s.
```

## 用法
Expand Down
16 changes: 13 additions & 3 deletions benchmark/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { join } from 'path'
import b from 'benny'
import { Summary } from 'benny/lib/internal/common-types'
import nodePinyin from 'pinyin'
import { pinyin as pinyinPro } from 'pinyin-pro'

import { pinyin } from '../index'

const short = '你好拼音'
const long = readFileSync(join(__dirname, 'long.txt'), 'utf-8')
const long = readFileSync(join(__dirname, 'long.txt'))
const longText = long.toString('utf8')

async function run() {
const output = [
Expand All @@ -19,6 +21,10 @@ async function run() {
pinyin(short)
}),

b.add('pinyin-pro', () => {
pinyinPro(short)
}),

b.add('node-pinyin', () => {
nodePinyin(short)
}),
Expand All @@ -34,8 +40,12 @@ async function run() {
pinyin(long)
}),

b.add('pinyin-pro', () => {
pinyinPro(longText)
}),

b.add('node-pinyin', () => {
nodePinyin(long)
nodePinyin(longText)
}),

b.cycle(),
Expand Down Expand Up @@ -64,7 +74,7 @@ async function run() {
}),

b.add('node-pinyin', () => {
nodePinyin(long, { segment: true })
nodePinyin(longText, { segment: true })
}),

b.cycle(),
Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ export interface PinyinConvertOptions {
heteronym?: boolean
segment?: boolean
}
export function pinyin(inputStr: string, opt?: PinyinConvertOptions | undefined | null): string[] | string[][]
export function asyncPinyin(input: string, opt?: PinyinConvertOptions | undefined | null, signal?: AbortSignal | undefined | null): Promise<string[] | string[][]>
export function pinyin(input: string | Buffer, opt?: PinyinConvertOptions | undefined | null): string[] | string[][]
export function asyncPinyin(input: string | Buffer, opt?: PinyinConvertOptions | undefined | null, signal?: AbortSignal | undefined | null): Promise<string[] | string[][]>
export function compare(inputA: string, inputB: string): number
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
},
"scripts": {
"artifacts": "napi artifacts",
"bench": "node -r ts-node/register/transpile-only benchmark/bench.ts",
"bench": "node -r @swc-node/register benchmark/bench.ts",
"build": "napi build --platform --release --pipe \"prettier -w\"",
"build:debug": "napi build --platform --pipe \"prettier -w\"",
"format": "run-p format:md format:json format:yaml format:source format:rs",
Expand Down Expand Up @@ -92,6 +92,7 @@
"npm-run-all": "^4.1.5",
"pinst": "^3.0.0",
"pinyin": "^2.11.2",
"pinyin-pro": "^3.14.0",
"prettier": "^2.8.1",
"typescript": "^5.0.0"
},
Expand Down
Loading

0 comments on commit 23f7307

Please sign in to comment.