In this article, I'm going to compare the efficiency of two powerful keyboard handlers in the Rust ecosystem, namely k_board and termion.
To determine which one is more efficient, I will compare the final footprint of a file with each development dependency running the same code:
Print 'UP!' if the up arrow key is pressed, print 'DOWN!' if the down arrow key is pressed, and exit the program if the enter key is pressed.
termion code & final space
use termion::input::TermRead;
use termion::raw::IntoRawMode;
use std::io::Write;
fn main() {
let mut stdout = std::io::stdout().into_raw_mode().unwrap();
let stdin: std::io::Stdin = std::io::stdin();
for c in stdin.keys() {
match c.unwrap() {
termion::event::Key::Up => println!("Up!"),
termion::event::Key::Down => println!("Down!"),
termion::event::Key::Char('\n') => break,
_ => {}
}
stdout.flush().unwrap();
}
}
mateo@debian:~/Dev$ du -h pruebas/
64K pruebas/.git/hooks
4,0K pruebas/.git/objects/pack
4,0K pruebas/.git/objects/info
12K pruebas/.git/objects
4,0K pruebas/.git/refs/heads
4,0K pruebas/.git/refs/tags
12K pruebas/.git/refs
8,0K pruebas/.git/info
112K pruebas/.git
12K pruebas/src
14M pruebas/target/release/deps
12K pruebas/target/release/.fingerprint/libc-f4899200925cd573
20K pruebas/target/release/.fingerprint/libc-0b715d722187b0c6
20K pruebas/target/release/.fingerprint/pruebas-801f0ed4b4a9017c
20K pruebas/target/release/.fingerprint/numtoa-5db795b61c1068b6
20K pruebas/target/release/.fingerprint/termion-57b99c4cdcd30adf
20K pruebas/target/release/.fingerprint/libc-1e1f3236a10c7892
116K pruebas/target/release/.fingerprint
4,0K pruebas/target/release/examples
4,0K pruebas/target/release/build/libc-f4899200925cd573/out
20K pruebas/target/release/build/libc-f4899200925cd573
4,6M pruebas/target/release/build/libc-1e1f3236a10c7892
4,6M pruebas/target/release/build
4,0K pruebas/target/release/incremental
18M pruebas/target/release
18M pruebas/target
19M pruebas/
19 MB memory space used.
k_board code & final space
fn main() {
for key in k_board::Keyboard::new() {
match key {
k_board::Keys::Up => println!("Up!"),
k_board::Keys::Down => println!("Down!"),
k_board::Keys::Enter => break,
_ => {}
}
}
}
mateo@debian:~/Dev$ du -h pruebas/
64K pruebas/.git/hooks
4,0K pruebas/.git/objects/pack
4,0K pruebas/.git/objects/info
12K pruebas/.git/objects
4,0K pruebas/.git/refs/heads
4,0K pruebas/.git/refs/tags
12K pruebas/.git/refs
8,0K pruebas/.git/info
112K pruebas/.git
8,0K pruebas/src
4,6M pruebas/target/release/deps
20K pruebas/target/release/.fingerprint/pruebas-3a8501bba3baba89
20K pruebas/target/release/.fingerprint/k_board-217281d3fbdf4f77
44K pruebas/target/release/.fingerprint
4,0K pruebas/target/release/examples
4,0K pruebas/target/release/build
4,0K pruebas/target/release/incremental
4,6M pruebas/target/release
4,6M pruebas/target
4,8M pruebas/
4,8 MB memory space used.
After performing the same task, we can see that k_board is 3.96 times lighter than termion. Additionally, it has a cleaner and more concise syntax.
Top comments (0)