Open Arcterus opened 10 years ago
When is it half a second slower?
Ah, sorry. It's half a second slower when running the code ten times in a row (using a loop). So, it's actually about 0.05 seconds slower, which should be easier to fix.
The code is basically a direct port of cksum
in uutils from getopts to litopts (I just followed the example files that you gave).
Can you post your code and the way you run it?
It was something like:
fn uumain(args: Vec<Vec<u8>>) {
static OPTS: litopts::Opts = litopts! {
/// print version info
"-V, --version",
/// print help menu
"-h, --help"
};
let rec = match OPTS.getopts(args.tail()) {
Ok(r) => r,
Err(o) => match o.var {
OptUnknown(o) => {
show_error!("unknown option: {}", o);
return 1;
}
_ => unreachable!()
}
};
for o in rec.res.iter() {
match o.as_str {
"h" => { /* help menu stuff */ }
"V" => { /* version info stuff */ }
_ => unreachable!()
}
}
for &filename in res.free.iter() {
// do stuff
}
}
This is just used in place of getopts in cksum
.
To test (after making relevant changes to cksum
and mkmain.rs
):
$ cd path/to/uutils
$ make BUILD=cksum
$ FILES=$(find . -name '*.rs') # NOTE: this is unnecessary to test this
$ FILES="$FILES $FILES $FILES $FILES" # one could just call build/cksum with
$ FILES="$FILES $FILES $FILES $FILES" # mkmain.rs
$ time for a in {1..10}; do build/cksum $FILES; done > /dev/null
After writing the code here, I think the slowdown might be the as_str
field. I'm assuming it converts Vec<u8>
s into &str
s?
EDIT: nvm, it looks like they're &'static str
s...
Consider the following program: https://gist.github.com/anonymous/b3a81a9105d451a8ab81
When called with 5M arguments, the litopts version executes in 16 seconds while the getopts version takes 18 seconds. When you reduce the number of arguments the difference becomes negligible.
I'll just assume that you are correct. I can't test this right now.
This is currently about half a second slower than getopts. It would be preferably to at least get its speed on par with getopts.