1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

seq: Directly write separator string, instead of using format

Doing `stdout.write_all(separator.as_bytes())?` is quite a bit
faster than using format to do the same operation:
`write!(stdout, "{separator}")?`.

This speeds up by about 10% on simple cases.

We do the same for the terminator even though this has no measurable
performance impact.
This commit is contained in:
Nicolas Boichat 2025-03-24 17:56:31 +01:00
parent 99de7bf30c
commit 66745427cb
2 changed files with 16 additions and 2 deletions

View file

@ -63,4 +63,18 @@ of system time). Simply wrapping `stdout` in a `BufWriter` increased performance
by about 2 times for a floating point increment test case, leading to similar
performance compared with GNU `seq`.
### Directly print strings
As expected, directly printing a string:
```rust
stdout.write_all(separator.as_bytes())?
```
is quite a bit faster than using format to do the same operation:
```rust
write!(stdout, "{separator}")?
```
The change above resulted in a ~10% speedup.
[0]: https://github.com/sharkdp/hyperfine

View file

@ -260,7 +260,7 @@ fn print_seq(
let mut is_first_iteration = true;
while !done_printing(&value, &increment, &last) {
if !is_first_iteration {
write!(stdout, "{separator}")?;
stdout.write_all(separator.as_bytes())?;
}
format.fmt(&mut stdout, &value)?;
// TODO Implement augmenting addition.
@ -268,7 +268,7 @@ fn print_seq(
is_first_iteration = false;
}
if !is_first_iteration {
write!(stdout, "{terminator}")?;
stdout.write_all(terminator.as_bytes())?;
}
stdout.flush()?;
Ok(())