What is fundamentally wrong with this code if it runs 3 times slower than similar Go code? What am I doing wrong? Yes, I ran it in release mode. Yes, it could be a one line code using collection traits. I just want to know where the bottleneck is.
struct Word {
text: String,
}
fn get_words(v: &Vec<u8>) -> Vec<Word> {
let mut words: Vec<Word> = Vec::new();
let mut lh: usize = 0;
for i in 0..v.len() {
if v[i].is_ascii_whitespace() {
if i > lh {
let s = String::from_utf8_lossy(&v[lh..i]).to_string();
words.push(Word { text: s });
}
lh = i + 1;
}
}
words
}
Here is its Go counterpart:
type Word struct {
Text string
}
func getWords(bs []byte) []Word {
var (
words []Word
lh int
)
for i := 0; i < len(bs); i++ {
b := bs[i]
if b == ' ' || b == '\r' || b == '\n' {
if i > lh {
words = append(words, Word{
Text: string(bs[lh:i]),
})
}
lh = i + 1
}
}
return words
}