13个稳定版本
1.5.0 | 2021年4月17日 |
---|---|
1.4.0 | 2021年2月13日 |
1.3.2 | 2021年1月10日 |
1.0.0 | 2020年12月31日 |
0.1.0 | 2020年12月31日 |
在值格式化分类中排名135
每月下载量36次
24KB
430 代码行
shortscale
Rust库,用于将数字转换为英文单词。
此模块是作为对JavaScript和Rust的探索而编写的,相关文档见此处。
短尺度,对于1000的每个幂有不同的单词。
此库表示从0到千、百万、十亿、兆和京,最高到999_999_999_999_999_999的数字。
函数
pub fn shortscale(num: u64) -> String
示例
use shortscale::shortscale;
assert_eq!(
shortscale(420_000_999_015),
"four hundred and twenty billion nine hundred \
and ninety nine thousand and fifteen"
);
字符串写入器
用于高效写入可变预分配的字符串。
性能差异很小,并且在不同系统上有所变化。
pub fn shortscale_string_writer(s: &mut String, num: u64)
use shortscale::shortscale_string_writer;
let mut my_string = String::from("The number 27 in words is ");
my_string.reserve(1024); // pre-allocate capacity (for performance only)
shortscale_string_writer(&mut my_string, 27);
assert_eq!(my_string, "The number 27 in words is twenty seven");
额外内容
作为我对Rust的第一次探索的记录,较老的实现保留在shortscale::extra下。要运行基准测试,请执行RUSTFLAGS="--cfg extra" cargo bench
。
GitHub Actions,在Ubuntu上运行。
test a_shortscale ... bench: 262 ns/iter (+/- 42)
test b_shortscale_string_writer_no_alloc ... bench: 99 ns/iter (+/- 17)
test c_str_push ... bench: 266 ns/iter (+/- 55)
test d_vec_push ... bench: 301 ns/iter (+/- 48)
test e_display_no_alloc ... bench: 254 ns/iter (+/- 55)
test f_vec_concat ... bench: 635 ns/iter (+/- 131)
test g_string_join ... bench: 632 ns/iter (+/- 80)
在MacOS Catalina 2.6 GHz Intel Core i7上,内存分配似乎要慢得多。
test a_shortscale ... bench: 378 ns/iter (+/- 59)
test b_shortscale_string_writer_no_alloc ... bench: 75 ns/iter (+/- 2)
test c_str_push ... bench: 396 ns/iter (+/- 42)
test d_vec_push ... bench: 435 ns/iter (+/- 12)
test e_display_no_alloc ... bench: 217 ns/iter (+/- 30)
test f_vec_concat ... bench: 2,087 ns/iter (+/- 79)
test g_string_join ... bench: 2,166 ns/iter (+/- 225)
JavaScript
在jldec/shortscale的JavaScript版本上运行npm run bench
显示,JavaScript也非常快 - 在MacOS上比我的第一个两个简单的Rust实现还要快。
Ubuntu,Node v14
20000 calls, 1200000 bytes, 1373 ns/call
20000 calls, 1200000 bytes, 1336 ns/call
20000 calls, 1200000 bytes, 1480 ns/call
MacOS
20000 calls, 1200000 bytes, 967 ns/call
20000 calls, 1200000 bytes, 982 ns/call
20000 calls, 1200000 bytes, 965 ns/call
版权所有2021年,Jürgen Leschner - github.com/jldec - MIT许可证