examples: make `word-count` example comparison fairer
This commit is contained in:
parent
0b269c4d3d
commit
06ff76fec2
|
@ -1,6 +1,3 @@
|
|||
// Source adopted from
|
||||
// https://github.com/tildeio/helix-website/blob/master/crates/word_count/src/lib.rs
|
||||
|
||||
use pyo3::prelude::*;
|
||||
use rayon::prelude::*;
|
||||
|
||||
|
@ -24,28 +21,11 @@ fn search_sequential_allow_threads(py: Python, contents: &str, needle: &str) ->
|
|||
py.allow_threads(|| search_sequential(contents, needle))
|
||||
}
|
||||
|
||||
fn matches(word: &str, needle: &str) -> bool {
|
||||
let mut needle = needle.chars();
|
||||
for ch in word.chars().skip_while(|ch| !ch.is_alphabetic()) {
|
||||
match needle.next() {
|
||||
None => {
|
||||
return !ch.is_alphabetic();
|
||||
}
|
||||
Some(expect) => {
|
||||
if ch.to_lowercase().next() != Some(expect) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
needle.next().is_none()
|
||||
}
|
||||
|
||||
/// Count the occurences of needle in line, case insensitive
|
||||
/// Count the occurrences of needle in line, case insensitive
|
||||
fn count_line(line: &str, needle: &str) -> usize {
|
||||
let mut total = 0;
|
||||
for word in line.split(' ') {
|
||||
if matches(word, needle) {
|
||||
if word == needle {
|
||||
total += 1;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
import pytest
|
||||
|
||||
import word_count
|
||||
|
||||
|
||||
|
|
|
@ -8,11 +8,10 @@ __all__ = [
|
|||
]
|
||||
|
||||
|
||||
def search_py(contents, needle):
|
||||
def search_py(contents: str, needle: str) -> int:
|
||||
total = 0
|
||||
for line in contents.split():
|
||||
words = line.split(" ")
|
||||
for word in words:
|
||||
for line in contents.splitlines():
|
||||
for word in line.split(" "):
|
||||
if word == needle:
|
||||
total += 1
|
||||
return total
|
||||
|
|
Loading…
Reference in New Issue