Comments (4)
@hannobraun do you have a preference between loops/no loops for this?
I have thrown together some generated code that does this (for each combo):
pub fn correct_ch_7_prf_64(dist_cm: u16) -> i16 {
let x = i16::from(dist_cm) + -30;
let y = if dist_cm < 100u16 { 0 }
else if dist_cm < 125u16 { 1 }
else if dist_cm < 150u16 { 4 }
else if dist_cm < 175u16 { 6 }
else if dist_cm < 200u16 { 8 }
else if dist_cm < 225u16 { 10 }
else if dist_cm < 250u16 { 12 }
else if dist_cm < 275u16 { 15 }
else if dist_cm < 300u16 { 17 }
else if dist_cm < 325u16 { 18 }
else if dist_cm < 350u16 { 20 }
else if dist_cm < 375u16 { 21 }
else if dist_cm < 400u16 { 22 }
else if dist_cm < 425u16 { 24 }
else if dist_cm < 450u16 { 25 }
else if dist_cm < 475u16 { 26 }
else if dist_cm < 500u16 { 28 }
else if dist_cm < 525u16 { 29 }
else if dist_cm < 550u16 { 30 }
else if dist_cm < 575u16 { 31 }
else if dist_cm < 600u16 { 32 }
else if dist_cm < 625u16 { 33 }
else if dist_cm < 650u16 { 34 }
else if dist_cm < 700u16 { 35 }
else if dist_cm < 725u16 { 36 }
else if dist_cm < 775u16 { 37 }
else if dist_cm < 825u16 { 38 }
else if dist_cm < 875u16 { 39 }
else if dist_cm < 925u16 { 40 }
else if dist_cm < 975u16 { 41 }
else if dist_cm < 1050u16 { 42 }
else if dist_cm < 1150u16 { 43 }
else if dist_cm < 1250u16 { 44 }
else if dist_cm < 1350u16 { 45 }
else if dist_cm < 1500u16 { 46 }
else if dist_cm < 1675u16 { 47 }
else if dist_cm < 1875u16 { 48 }
else if dist_cm < 2075u16 { 49 }
else if dist_cm < 2250u16 { 50 }
else if dist_cm < 2375u16 { 51 }
else if dist_cm < 2500u16 { 52 }
else if dist_cm < 2750u16 { 53 }
else if dist_cm < 3375u16 { 54 }
else if dist_cm < 3825u16 { 55 }
else if dist_cm < 4300u16 { 56 }
else if dist_cm < 4800u16 { 57 }
else { 58 };
x + y
}
But I could also probably do something like this:
const CH_7_PRF_64: &[(u16, u16)] = &[
(100, 0),
(125, 1),
// ...
];
pub fn correct_ch_7_prf_64(dist_cm: u16) -> i16 {
for (dist, correction) in CH_7_PRF_64 {
if dist_cm < dist {
return i16::from(dist_cm) + -30 + correction;
}
}
// todo final case
}
from rust-dwm1001.
Generally, without taking into account the optimizer (lol), I expect the first version to be faster, but take more code space (marginally), and I expect the second to be slower, but take less code space (marginally). The second form is also much more readable, but for generated code, not sure if that is important.
from rust-dwm1001.
I don't have a strong preference, but the loop-based version seems more attractive:
- More readable is always good, generated or not.
- Less code size is always good. Performance is probably not critical in this case as it only needs to happen once per measurement, and a measurement involves I/O (at least 3 messages being sent between two nodes).
But hard to say, especially without knowing what the compiler is going to do.
Have you considered match statements? Might give the compiler more room for optimization than if
statements (not sure though, this is mostly baseless intuition).
from rust-dwm1001.
Closing in favor of jkelleyrtp/dw1000-rs#105.
from rust-dwm1001.
Related Issues (12)
- Consider adding board to bobbin-sdk HOT 2
- Add type parameter to `Led` to identify pin HOT 1
- `dw1000_id` example doesn't work with LLD HOT 1
- Add support for the buttons on the dev board HOT 2
- Build currently broken due to missing git dep HOT 1
- Current build is broken due to ieee802154 changes HOT 4
- Enable correct feature for nrf52832-hal dependency
- Ranging examples (possibly) don't work in release mode HOT 1
- The DWM1001 struct can't be created with RTFM (safely) HOT 4
- Make DW_IRQ and DW_RST constructors public HOT 2
- Use latest version of embedded-hal traits HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust-dwm1001.