|
1 | | -# ShortStrings |
2 | | -This is an efficient string format for storing strings using integer types. For example, `UInt32` can hold 3 bytes of string with 1 byte to record the size of the string and a `UInt128` can hold a byte string with 1 byte to record the size of the string. |
3 | | - |
4 | | -Using BitIntegers.jl, integer of larger size than `UInt128` can be defined. This package support string with up to 126 bytes in size. |
5 | | - |
6 | | -# Quick Start |
7 | | -```julia |
8 | | -using ShortStrings, SortingAlgorithms |
9 | | -N = Int(1e6) |
10 | | -svec = [randstring(rand(1:15)) for i=1:N] |
11 | | -# convert to ShortString |
12 | | -ssvec = ShortString15.(svec) |
13 | | -@time sort(svec); |
14 | | -@time sort(ssvec, by = x->x.size_content, alg=RadixSort); |
15 | | - |
16 | | -# conversion to shorter strings is also possible with |
17 | | -ShortString7(randstring(7)) |
18 | | -ShortString3(randstring(3)) |
19 | | - |
20 | | -# convenience macros are provided for writing actual strings (e.g., for comparison) |
21 | | -s15 = ss15"A short string" # ShortString15 === ShortString{Int128} |
22 | | -s7 = ss7"shorter" # ShortString7 === ShortString{Int64} |
23 | | -s3 = ss3"srt" # ShortString3 === ShortString{Int32} |
24 | | -``` |
25 | | - |
26 | | -# Benchmark |
27 | | - |
28 | | - |
29 | | - |
30 | | -## Benchmarking code |
31 | | -```julia |
32 | | -using SortingLab, ShortStrings, SortingAlgorithms, BenchmarkTools; |
33 | | -N = Int(1e6); |
34 | | -svec = [randstring(rand(1:15)) for i=1:N]; |
35 | | -# convert to ShortString |
36 | | -ssvec = ShortString15.(svec); |
37 | | -basesort = @benchmark sort($svec) |
38 | | -radixsort_timings = @benchmark SortingLab.radixsort($svec) |
39 | | -short_radixsort = @benchmark ShortStrings.fsort($ssvec) |
40 | | -# another way to do sorting |
41 | | -sort(ssvec, by = x->x.size_content, alg=RadixSort) |
42 | | - |
43 | | -using RCall |
44 | | -R""" |
45 | | -memory.limit(2^31-1) |
46 | | -""" |
47 | | -@rput svec; |
48 | | -r_timings = R""" |
49 | | -memory.limit(2^31-1) |
50 | | -replicate($(length(short_radixsort.times)), system.time(sort(svec, method="radix"))[3]) |
51 | | -"""; |
52 | | - |
53 | | -using Plots |
54 | | -bar(["Base.sort","SortingLab.radixsort","ShortStrings radix sort", "R radix sort"], |
55 | | - mean.([basesort.times./1e9, radixsort_timings.times./1e9, short_radixsort.times./1e9, r_timings]), |
56 | | - title="String sort performance - len: 1m, variable size 15", |
57 | | - label = "seconds") |
58 | | -savefig("readme_string_sort.png") |
59 | | - |
60 | | - |
61 | | -using SortingLab, ShortStrings, SortingAlgorithms, BenchmarkTools; |
62 | | -N = Int(2e7); |
63 | | -svec = rand([randstring(rand(1:15)) for i=1:N÷100],N) |
64 | | -# convert to ShortString |
65 | | -ssvec = ShortString15.(svec); |
66 | | -basesort = @benchmark sort($svec) samples = 5 seconds = 120 |
67 | | -radixsort_timings = @benchmark SortingLab.radixsort($svec) samples = 5 seconds = 120 |
68 | | -short_radixsort = @benchmark ShortStrings.fsort($ssvec) samples = 5 seconds = 120 |
69 | | - |
70 | | -using RCall |
71 | | - |
72 | | -@rput svec; |
73 | | -r_timings = R""" |
74 | | -replicate(max(5, $(length(short_radixsort.times))), system.time(sort(svec, method="radix"))[3]) |
75 | | -"""; |
76 | | - |
77 | | -using Plots |
78 | | -bar(["Base.sort","SortingLab.radixsort","ShortStrings radix sort", "R radix sort"], |
79 | | - mean.([basesort.times./1e9, radixsort_timings.times./1e9, short_radixsort.times./1e9, r_timings]), |
80 | | - title="String sort performance - len: $(N÷1_000_000)m, fixed size: 15", |
81 | | - label = "seconds") |
82 | | -savefig("readme_string_sort_fixed_len.png") |
83 | | -``` |
84 | | - |
85 | | -# Notes |
86 | | -This is based on the discussion [here](https://discourse.julialang.org/t/progress-towards-faster-sortperm-for-strings/8505/4?u=xiaodai). If Julia.Base adopts the hybrid representation of strings then it makes this package redundant. |
87 | | - |
88 | | -# Build Status |
89 | | - |
90 | | -[](https://travis-ci.org/xiaodaigh/ShortStrings.jl) |
91 | | - |
92 | | -[](https://coveralls.io/github/xiaodaigh/ShortStrings.jl?branch=master) |
93 | | - |
94 | | -[](http://codecov.io/github/xiaodaigh/ShortStrings.jl?branch=master) |
| 1 | +## ShortStrings |
| 2 | +This is an efficient string format for storing strings using integer types. For example, `UInt32` can hold 3 bytes of string with 1 byte to record the size of the string and a `UInt128` can hold a byte string with 1 byte to record the size of the string. |
| 3 | + |
| 4 | +Using BitIntegers.jl, integer of larger size than `UInt128` can be defined. This package support string with up to 126 bytes in size. |
| 5 | + |
| 6 | +## Quick Start |
| 7 | +````julia |
| 8 | + |
| 9 | +using ShortStrings |
| 10 | + |
| 11 | +using SortingAlgorithms |
| 12 | +using Random: randstring |
| 13 | + |
| 14 | +N = Int(1e6) |
| 15 | +svec = [randstring(rand(1:15)) for i=1:N] |
| 16 | +# convert to ShortString |
| 17 | +ssvec = ShortString15.(svec) |
| 18 | +@time sort(svec); |
| 19 | +@time sort(ssvec, by = x->x.size_content, alg=RadixSort); |
| 20 | + |
| 21 | +# conversion to shorter strings is also possible with |
| 22 | +ShortString7(randstring(7)) |
| 23 | +ShortString3(randstring(3)) |
| 24 | + |
| 25 | +# convenience macros are provided for writing actual strings (e.g., for comparison) |
| 26 | +s15 = ss15"A short string" # ShortString15 === ShortString{Int128} |
| 27 | +s7 = ss7"shorter" # ShortString7 === ShortString{Int64} |
| 28 | +s3 = ss3"srt" # ShortString3 === ShortString{Int32} |
| 29 | +```` |
| 30 | + |
| 31 | + |
| 32 | +```` |
| 33 | +0.339147 seconds (131 allocations: 11.451 MiB) |
| 34 | + 0.359553 seconds (784.87 k allocations: 71.526 MiB, 13.41% gc time) |
| 35 | +"srt" |
| 36 | +```` |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +## Benchmarks |
| 43 | + |
| 44 | +<details> |
| 45 | +<summary><b>Code</b></summary> |
| 46 | +````julia |
| 47 | + |
| 48 | +using SortingLab, ShortStrings, SortingAlgorithms, BenchmarkTools; |
| 49 | +N = Int(1e6); |
| 50 | +svec = [randstring(rand(1:15)) for i=1:N]; |
| 51 | +# convert to ShortString |
| 52 | +ssvec = ShortString15.(svec); |
| 53 | +basesort = @benchmark sort($svec) |
| 54 | +radixsort_timings = @benchmark SortingLab.radixsort($svec) |
| 55 | +short_radixsort = @benchmark ShortStrings.fsort($ssvec) |
| 56 | +# another way to do sorting |
| 57 | +sort(ssvec, by = x->x.size_content, alg=RadixSort) |
| 58 | + |
| 59 | +using RCall |
| 60 | +R""" |
| 61 | +memory.limit(2^31-1) |
| 62 | +""" |
| 63 | +@rput svec; |
| 64 | +r_timings = R""" |
| 65 | +memory.limit(2^31-1) |
| 66 | +replicate($(length(short_radixsort.times)), system.time(sort(svec, method="radix"))[3]) |
| 67 | +"""; |
| 68 | + |
| 69 | +using Plots |
| 70 | +bar(["Base.sort","SortingLab.radixsort","ShortStrings radix sort", "R radix sort"], |
| 71 | + mean.([basesort.times./1e9, radixsort_timings.times./1e9, short_radixsort.times./1e9, r_timings]), |
| 72 | + title="String sort performance - len: 1m, variable size 15", |
| 73 | + label = "seconds") |
| 74 | +```` |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +</details> |
| 81 | + |
| 82 | +<details> |
| 83 | +<summary><b>Code</b></summary> |
| 84 | +````julia |
| 85 | + |
| 86 | +using SortingLab, ShortStrings, SortingAlgorithms, BenchmarkTools; |
| 87 | +N = Int(2e7); |
| 88 | +svec = rand([randstring(rand(1:15)) for i=1:N÷100],N) |
| 89 | +# convert to ShortString |
| 90 | +ssvec = ShortString15.(svec); |
| 91 | +basesort = @benchmark sort($svec) samples = 5 seconds = 120 |
| 92 | +radixsort_timings = @benchmark SortingLab.radixsort($svec) samples = 5 seconds = 120 |
| 93 | +short_radixsort = @benchmark ShortStrings.fsort($ssvec) samples = 5 seconds = 120 |
| 94 | + |
| 95 | +using RCall |
| 96 | + |
| 97 | +@rput svec; |
| 98 | +r_timings = R""" |
| 99 | +replicate(max(5, $(length(short_radixsort.times))), system.time(sort(svec, method="radix"))[3]) |
| 100 | +"""; |
| 101 | + |
| 102 | +using Plots |
| 103 | +bar(["Base.sort","SortingLab.radixsort","ShortStrings radix sort", "R radix sort"], |
| 104 | + mean.([basesort.times./1e9, radixsort_timings.times./1e9, short_radixsort.times./1e9, r_timings]), |
| 105 | + title="String sort performance - len: $(N÷1_000_000)m, fixed size: 15", |
| 106 | + label = "seconds") |
| 107 | +```` |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +</details> |
| 114 | + |
| 115 | +## Notes |
| 116 | +This is based on the discussion [here](https://discourse.julialang.org/t/progress-towards-faster-sortperm-for-strings/8505/4?u=xiaodai). If Julia.Base adopts the hybrid representation of strings then it makes this package redundant. |
| 117 | + |
| 118 | +# Build Status |
| 119 | + |
| 120 | +[](https://travis-ci.org/xiaodaigh/ShortStrings.jl) |
| 121 | + |
| 122 | +[](https://coveralls.io/github/xiaodaigh/ShortStrings.jl?branch=master) |
| 123 | + |
| 124 | +[](http://codecov.io/github/xiaodaigh/ShortStrings.jl?branch=master) |
0 commit comments