Parse the temperature in reverse#11
Open
irobot wants to merge 5 commits intojonhoo:mainfrom
Open
Conversation
Starting from the end of the line, take the digits one at a time.
For example the following line:
> Seattle;-12.3
* Temperature * 10 (T) is encoded as an `i16`.
* Starting from the end, the first digit is 3. T = 3
* Skip over the dot, T = 3
* Next is 2. T = 2 * 10 + 3 = 23
* Next character could be a digit, but it could also be - or ;
In this case it is 1, so T = 1 * 100 + T = 123
* One last time, we need to check for a -, which we have, so T = -T =
-123
* Next character is guaranteed to be ; thus we have also obtained the length
of the station name.
With this change, Hyperfine reports a ~1.50 faster finish time.
Tested on a 12 core / 24 thread AMD Ryzen 5900x.
> Benchmark 1: target/release/brrr-org
Time (mean ± σ): 2.205 s ± 0.020 s [User: 42.833 s, System: 1.119 s]
Range (min … max): 2.194 s … 2.260 s 10 runs
Benchmark 2: target/release/brrr
Time (mean ± σ): 1.473 s ± 0.021 s [User: 25.448 s, System: 1.108 s]
Range (min … max): 1.452 s … 1.529 s 10 runs
Summary
target/release/brrr ran
1.50 ± 0.03 times faster than target/release/brrr-org
Owner
|
Oh interesting! Give it a try after the optimizations from #2, which already sped us up a decent amount and see if it still improves things. |
Author
|
Yes, you're right! After merging in the latest improvements, speed up is now only ~5%. My feeling is that most of the gains were in minimizing/removing the search for the semicolon. |
+ move temperature parsing code to parse_temperature
+ restore temp parsing unit tests
+ replace usize decrement with unchecked_sub() just in case, though it
doesn't seem to make any meaningful difference perf-wise.
After merging in the latest improvements, the speed up offered by this
change is no longer as significant.
```
Benchmark 1: target/release/brrr-main
Time (mean ± σ): 1.196 s ± 0.006 s [User: 19.190 s, System: 1.133 s]
Range (min … max): 1.186 s … 1.207 s 10 runs
Benchmark 2: target/release/brrr
Time (mean ± σ): 1.143 s ± 0.005 s [User: 17.915 s, System: 1.150 s]
Range (min … max): 1.135 s … 1.150 s 10 runs
Summary
target/release/brrr ran
1.05 ± 0.01 times faster than target/release/brrr-main
```
Wondering if CI failing has to do with rustfmt using a different style edition.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The observation is that the temperature component is ~fixed length (between 3 and 5 characters).
Therefore having found the end of the line, scanning from the end, we obtain both the temperature and the location of the semicolon.
With this change, Hyperfine reports a ~1.50 faster finish time.
Tested on a 12 core / 24 thread AMD Ryzen 5900x.
Benchmark 2: target/release/brrr
Time (mean ± σ): 1.473 s ± 0.021 s [User: 25.448 s, System: 1.108 s]
Range (min … max): 1.452 s … 1.529 s 10 runs
Summary
target/release/brrr ran
1.50 ± 0.03 times faster than target/release/brrr-org