File tree Expand file tree Collapse file tree 1 file changed +4
-0
lines changed
Expand file tree Collapse file tree 1 file changed +4
-0
lines changed Original file line number Diff line number Diff line change @@ -1069,15 +1069,18 @@ How Fast Digit Counting Works
10691069The Problem: Need to know buffer size before converting number to string
10701070
10711071Traditional Approach:
1072+ ``` cpp
10721073size_t digit_count (uint64_t v) {
10731074 return std::to_string(v).length();
10741075 // 1. Allocates memory
10751076 // 2. Converts entire number to string
10761077 // 3. Gets length
10771078 // 4. Deallocates string
10781079}
1080+ ```
10791081
10801082Our Optimization:
1083+ ```cpp
10811084int fast_digit_count(uint64_t x) {
10821085 // Approximate using bit operations (no division!)
10831086 int y = (19 * int_log2(x) >> 6);
@@ -1088,6 +1091,7 @@ int fast_digit_count(uint64_t x) {
10881091
10891092 return y + 1;
10901093}
1094+ ```
10911095
10921096Zero allocations, no string conversion, just math!
10931097
You can’t perform that action at this time.
0 commit comments