Optimizing a Math Expression Parser in Rust
rpallas.xyz
Optimizing a Math Expression Parser in Rust
1–10 of 60 posts
Re: Optimizing a Math Expression Parser in Rust
#2Right now, realistic can parse "(* (^ 40 0.5) (^ 90 0.5))" and it will tell you that's 60, because yeah, it's sixty, that's how real arithmetic works.
But it would be nice to write "(40^0.5) * (90^0.5)" or similar and have that work instead or as well. The months of work on realistic meant I spent so long without a "natural" parser that I got used to this.
Re: Optimizing a Math Expression Parser in Rust
#3Re: Optimizing a Math Expression Parser in Rust
#4Re: Optimizing a Math Expression Parser in Rust
#5- cpu time (better CPU usage can mean shorter wall time but higher CPU)
- memory usage
- but also and maybe more interestingly complexity of code (not an absolute metric, but a very complex/non portable code for 5% speedup may or may not be worth it)
EDIT: formatting
Re: Optimizing a Math Expression Parser in Rust
#6Edited: In the optimized version the author use bytes and generators and avoid using strings. I don't know if Rust generators are optimized for speed or memory, ideally you could define the length of the buffer according to the memory cache available.
Edited: I find strange using input = read_input_file()? and then using eval(&input), what happens when there is an error reading the file? Rust is supposed to be a high security language. In CL there are keyword like if-does-not-exists to decide what to do and also read accepts additional parameters for end-of-file and for expressing that this read is inside a recursive procedure inside another read.
I should stop comparing Rust to CL, better learn Rust first. I consider this kind of articles a very good way of learning Rust for those interested in parsing and optimizations. Rust seems to be a very nice language when you can afford the time to develop your program.
Re: Optimizing a Math Expression Parser in Rust
#7 n => Token::Operand(n.parse().unwrap()),
How does the compiler derive the type of n?Re: Optimizing a Math Expression Parser in Rust
#8Can somebody explain this line: n => Token::Operand(n.parse().unwrap()), How does the compiler derive the type of n?
Re: Optimizing a Math Expression Parser in Rust
#9Can somebody explain this line: n => Token::Operand(n.parse().unwrap()), How does the compiler derive the type of n?
That value is an item from the iterator we got from calling split_whitespace() and split_whitespace() returns a SplitWhiteSpace, a custom iterator whose items are themselves sub-strings of the input string with (no surprise) no white space in them. In Rust's terminology these are &str, references to a string slice.
So, the type is &str