Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::time::Instant;
- fn get_char_by_bytes(val: &String, index: usize) -> char {
- val.as_bytes()[index] as char
- }
- fn get_nth_character(val: &String, index: usize) -> char {
- val.chars().nth(index).unwrap()
- }
- fn main() {
- let word = String::from("Hello world").repeat(1000);
- // let index = 10usize;
- // let answer = 'o';
- let time_limits = (3..=7).map(|x| 10u32.pow(x));
- let time_limit = time_limits.into_iter().nth(0).unwrap();
- for index in 990..1000 {
- println!("For iteration {}, index {}", time_limit, index);
- let now = Instant::now();
- for _ in 0..time_limit {
- get_char_by_bytes(&word, index);
- }
- let elapsed = now.elapsed();
- println!("Elapsed time for bytes {:2?}", elapsed);
- let now = Instant::now();
- for _ in 0..time_limit {
- get_nth_character(&word, index);
- }
- let elapsed2 = now.elapsed();
- println!("Elapsed time for nth character {:2?}", elapsed2);
- println!(
- "Ratio of slowdown {:.2}",
- ((elapsed2.as_micros() as f64) / (elapsed.as_micros() as f64))
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement