mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-16 01:43:35 +00:00
In C grades, uint32_to_string/1 is currently implemented by doing the
following:
1. Calling sprintf() to write the string into a buffer on the stack.
2. Calling strlen() on the buffer to determine the actual number of digits.
3. Allocate the appropriate amount of space on the heap.
4. Copying the string from the buffer on to the heap.
The benchmark included in this diff contains a number of alternative
implementations, all of which avoid much of the above overhead.
All of these implementations work by computing the required number of digits,
allocating space on the heap and then writing the digits into the string
backwards.
library/string.m:
Replace uint32_to_string with a faster implementation.
(This is alternative 1 in the benchmark program below.)
tests/hard_coded/Mmakefile:
tests/hard_coded/uint32_to_string.{m,exp}:
A test of uint32 to string.
benchmarks/progs/integer_to_string/uint32_conversion.m:
A program for benchmarking various uint32 to string conversion
implementations.
47 lines
272 B
Plaintext
47 lines
272 B
Plaintext
0
|
|
1
|
|
2
|
|
7
|
|
8
|
|
9
|
|
10
|
|
11
|
|
99
|
|
100
|
|
101
|
|
127
|
|
128
|
|
129
|
|
254
|
|
255
|
|
256
|
|
999
|
|
1000
|
|
1001
|
|
9999
|
|
10000
|
|
10001
|
|
32766
|
|
32767
|
|
32768
|
|
65534
|
|
65535
|
|
65536
|
|
99999
|
|
100000
|
|
100001
|
|
999999
|
|
1000000
|
|
1000001
|
|
9999999
|
|
10000000
|
|
10000001
|
|
99999999
|
|
100000000
|
|
100000001
|
|
999999999
|
|
1000000000
|
|
1000000001
|
|
2147483647
|
|
4294967295
|