parent
2c0a866029
commit
a923f8b5d3
|
@ -11,5 +11,5 @@ python setup.py install
|
||||||
```python
|
```python
|
||||||
from word_count_cls import WordCounter
|
from word_count_cls import WordCounter
|
||||||
|
|
||||||
WordCounter().search('path/to/file', 'word')
|
WordCounter("path/to/file).search('word')
|
||||||
```
|
```
|
|
@ -13,17 +13,17 @@ use rayon::prelude::*;
|
||||||
use pyo3::prelude::*;
|
use pyo3::prelude::*;
|
||||||
|
|
||||||
#[py::class]
|
#[py::class]
|
||||||
struct Words {
|
struct WordCounter {
|
||||||
path: String,
|
path: String,
|
||||||
token: PyToken,
|
token: PyToken,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[py::methods]
|
#[py::methods]
|
||||||
impl Words {
|
impl WordCounter {
|
||||||
|
|
||||||
#[new]
|
#[new]
|
||||||
fn __new__(obj: &PyRawObject, path: String) -> PyResult<()> {
|
fn __new__(obj: &PyRawObject, path: String) -> PyResult<()> {
|
||||||
obj.init(|t| Words {path: path, token: t})
|
obj.init(|t| WordCounter {path: path, token: t})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn search(&self, py: Python, search: String) -> PyResult<i32> {
|
fn search(&self, py: Python, search: String) -> PyResult<i32> {
|
||||||
|
@ -85,7 +85,7 @@ fn wc_parallel(lines: &str, search: &str) -> i32 {
|
||||||
|
|
||||||
#[py::modinit(_word_count)]
|
#[py::modinit(_word_count)]
|
||||||
fn init_mod(_py: Python, m: &PyModule) -> PyResult<()> {
|
fn init_mod(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||||
m.add_class::<Words>()?;
|
m.add_class::<WordCounter>()?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,12 +41,12 @@ Namespaces are one honking great idea -- let's do more of those!\n''' * 1000
|
||||||
|
|
||||||
|
|
||||||
def test_word_count_rust_parallel(benchmark):
|
def test_word_count_rust_parallel(benchmark):
|
||||||
count = benchmark(word_count_cls.Words(path).search, 'is')
|
count = benchmark(word_count_cls.WordCounter(path).search, 'is')
|
||||||
assert count == 10000
|
assert count == 10000
|
||||||
|
|
||||||
|
|
||||||
def test_word_count_rust_sequential(benchmark):
|
def test_word_count_rust_sequential(benchmark):
|
||||||
count = benchmark(word_count_cls.Words(path).search_sequential, 'is')
|
count = benchmark(word_count_cls.WordCounter(path).search_sequential, 'is')
|
||||||
assert count == 10000
|
assert count == 10000
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
from ._word_count import Words
|
from ._word_count import WordCounter
|
||||||
|
|
||||||
__all__ = ['Words', 'search_py']
|
__all__ = ['WordCounter', 'search_py']
|
||||||
|
|
||||||
|
|
||||||
def search_py(path, needle):
|
def search_py(path, needle):
|
||||||
|
|
Loading…
Reference in a new issue