| 1 |
# Benchmarking `tr` |
| 2 |
|
| 3 |
<!-- spell-checker:ignore aeiou --> |
| 4 |
|
| 5 |
`tr` performance is critical for large data processing pipelines. The implementation uses lookup tables for O(1) character operations. |
| 6 |
|
| 7 |
## Building |
| 8 |
|
| 9 |
```shell |
| 10 |
cargo build --release --package uu_tr |
| 11 |
``` |
| 12 |
|
| 13 |
## Basic Benchmarks |
| 14 |
|
| 15 |
### Character Translation |
| 16 |
|
| 17 |
```shell |
| 18 |
# Create test data |
| 19 |
dd if=/dev/zero bs=1M count=10 | tr '\0' 'x' > 10mb_input |
| 20 |
|
| 21 |
# Benchmark translation |
| 22 |
hyperfine --warmup 3 \ |
| 23 |
'tr a b < 10mb_input > /dev/null' \ |
| 24 |
'./target/release/tr a b < 10mb_input > /dev/null' |
| 25 |
``` |
| 26 |
|
| 27 |
### Character Deletion |
| 28 |
|
| 29 |
```shell |
| 30 |
# Create mixed input |
| 31 |
yes "abcdefghijklmnopqrstuvwxyz" | head -n 1000000 > mixed_input |
| 32 |
|
| 33 |
# Benchmark deletion |
| 34 |
hyperfine --warmup 3 \ |
| 35 |
'tr -d aeiou < mixed_input > /dev/null' \ |
| 36 |
'./target/release/tr -d aeiou < mixed_input > /dev/null' |
| 37 |
``` |
| 38 |
|
| 39 |
### Character Classes |
| 40 |
|
| 41 |
```shell |
| 42 |
# Benchmark character classes |
| 43 |
hyperfine --warmup 3 \ |
| 44 |
'tr "[:lower:]" "[:upper:]" < mixed_input > /dev/null' \ |
| 45 |
'./target/release/tr "[:lower:]" "[:upper:]" < mixed_input > /dev/null' |
| 46 |
``` |
| 47 |
|
| 48 |
## Quick Regression Test |
| 49 |
|
| 50 |
```shell |
| 51 |
#!/bin/bash |
| 52 |
cargo build --release --package uu_tr |
| 53 |
|
| 54 |
echo "=== Translation ===" |
| 55 |
hyperfine 'tr a b < 10mb_input > /dev/null' './target/release/tr a b < 10mb_input > /dev/null' |
| 56 |
|
| 57 |
echo "=== Deletion ===" |
| 58 |
hyperfine 'tr -d aeiou < mixed_input > /dev/null' './target/release/tr -d aeiou < mixed_input > /dev/null' |
| 59 |
``` |
| 60 |
|
| 61 |
## Performance Notes |
| 62 |
|
| 63 |
- Uses lookup tables instead of hash maps for O(1) operations |
| 64 |
- 32KB I/O buffers for improved throughput |
| 65 |
- Should be competitive with GNU `tr` for most operations |