Attempt to batch things.
This commit is contained in:
parent
3a7429df1a
commit
bb55a922b9
14
src/lib.rs
14
src/lib.rs
|
@ -1,19 +1,27 @@
|
||||||
#![feature(specialization)]
|
#![feature(specialization)]
|
||||||
|
|
||||||
#[macro_use] extern crate pyo3;
|
#[macro_use]
|
||||||
|
extern crate pyo3;
|
||||||
|
|
||||||
use pyo3::prelude::*;
|
use pyo3::prelude::*;
|
||||||
|
|
||||||
|
|
||||||
#[pyfunction]
|
#[pyfunction]
|
||||||
fn convert_celsius_to_fahrenheit(celsius: f32) -> f32 {
|
fn convert_celsius_to_fahrenheit(celsius: f32) -> f32 {
|
||||||
celsius * 1.8 + 32.0
|
celsius * 1.8 + 32.0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[pyfunction]
|
||||||
|
fn batch_convert_celsius_to_fahrenheit(celsius: Vec<f32>) -> Vec<f32> {
|
||||||
|
celsius
|
||||||
|
.iter()
|
||||||
|
.map(|temperature| convert_celsius_to_fahrenheit(temperature.clone()))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
#[pymodule]
|
#[pymodule]
|
||||||
fn unit_converter(_py: Python, m: &PyModule) -> PyResult<()> {
|
fn unit_converter(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||||
m.add_wrapped(wrap_pyfunction!(convert_celsius_to_fahrenheit))?;
|
m.add_wrapped(wrap_pyfunction!(convert_celsius_to_fahrenheit))?;
|
||||||
|
m.add_wrapped(wrap_pyfunction!(batch_convert_celsius_to_fahrenheit))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
19
test.py
19
test.py
|
@ -1,12 +1,23 @@
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
import unit_converter
|
import unit_converter
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module")
|
||||||
|
def batch_numbers():
|
||||||
|
return [random.random() * 100 for x in range(1000)]
|
||||||
|
|
||||||
|
|
||||||
def python_unit_converter(celsius):
|
def python_unit_converter(celsius):
|
||||||
return celsius * 1.8 + 32.0
|
return celsius * 1.8 + 32.0
|
||||||
|
|
||||||
|
|
||||||
|
def batch_python_unit_converter(temperatures):
|
||||||
|
return [celsius * 1.8 + 32.0 for celsius in temperatures]
|
||||||
|
|
||||||
|
|
||||||
def test_using_unit_converter():
|
def test_using_unit_converter():
|
||||||
assert unit_converter.convert_celsius_to_fahrenheit(25.0) == 77.0
|
assert unit_converter.convert_celsius_to_fahrenheit(25.0) == 77.0
|
||||||
assert python_unit_converter(25.0) == 77.0
|
assert python_unit_converter(25.0) == 77.0
|
||||||
|
@ -32,3 +43,11 @@ def test_using_rust(benchmark):
|
||||||
temperature = random.random() * 100
|
temperature = random.random() * 100
|
||||||
result = benchmark(unit_converter.convert_celsius_to_fahrenheit, temperature)
|
result = benchmark(unit_converter.convert_celsius_to_fahrenheit, temperature)
|
||||||
assert round(result, 3) == round(python_unit_converter(temperature), 3)
|
assert round(result, 3) == round(python_unit_converter(temperature), 3)
|
||||||
|
|
||||||
|
|
||||||
|
def test_using_python_batch(benchmark, batch_numbers):
|
||||||
|
benchmark(batch_python_unit_converter, batch_numbers)
|
||||||
|
|
||||||
|
|
||||||
|
def test_using_rust_batch(benchmark, batch_numbers):
|
||||||
|
benchmark(unit_converter.batch_convert_celsius_to_fahrenheit, batch_numbers)
|
||||||
|
|
Loading…
Reference in New Issue