Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
302 changes: 126 additions & 176 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@ crate-type = ["rlib", "cdylib"]
doctest = false

[dependencies]
pyo3 = "0.20"
numpy = "0.20"
pyo3 = "0.25"
numpy = "0.25"
time = { version = "0.3", features = ["parsing", "macros"] }
ndarray = "0.15"
# num-complex = "0.4"

[dev-dependencies]
assert_approx_eq = "1.1"
rstest = { version = "0.18.2", default-features = false }
pyo3 = { version = "0.20", features = ["auto-initialize"]}
rstest = { version = "0.25", default-features = false }
pyo3 = { version = "0.25", features = ["auto-initialize"]}

[features]
nonumpy = []
47 changes: 31 additions & 16 deletions benches/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

extern crate test;

use pyo3::{types::PyModule, Python};
use pyo3::{
ffi::c_str,
types::{PyAnyMethods, PyModule},
Python,
};
use std::ffi::CStr;
use test::{black_box, Bencher};

#[path = "../tests/common/mod.rs"]
mod common;
use common::PaymentsLoader;

// https://stackoverflow.com/questions/8919718/financial-python-library-that-has-xirr-and-xnpv-function
const PURE_PYTHON_IMPL: &str = r#"
const PURE_PYTHON_IMPL: &CStr = c_str!(
r#"
def xirr(transactions):
years = [(ta[0] - transactions[0][0]).days / 365.0 for ta in transactions]
residual = 1
Expand All @@ -30,9 +36,11 @@ def xirr(transactions):
guess -= step
step /= 2.0
return guess
"#;
"#
);

const SCIPY_IMPL: &str = r#"
const SCIPY_IMPL: &CStr = c_str!(
r#"
import scipy.optimize

def xnpv(rate, values, dates):
Expand All @@ -46,15 +54,16 @@ def xirr(values, dates):
return scipy.optimize.newton(lambda r: xnpv(r, values, dates), 0.0)
except RuntimeError: # Failed to converge?
return scipy.optimize.brentq(lambda r: xnpv(r, values, dates), -1.0, 1e10)
"#;
"#
);

macro_rules! bench_rust {
($name:ident, $file:expr) => {
#[bench]
fn $name(b: &mut Bencher) {
Python::with_gil(|py| {
let data = PaymentsLoader::from_csv(py, $file).to_records();
b.iter(|| pyxirr_call_impl!(py, "xirr", black_box((data,))).unwrap());
b.iter(|| pyxirr_call_impl!(py, "xirr", black_box((&data,))).unwrap());
});
}
};
Expand All @@ -65,12 +74,13 @@ macro_rules! bench_scipy {
#[bench]
fn $name(b: &mut Bencher) {
Python::with_gil(|py| {
let xirr = PyModule::from_code(py, SCIPY_IMPL, "xirr.py", "scipy_py_xirr")
.unwrap()
.getattr("xirr")
.unwrap();
let py_mod =
PyModule::from_code(py, SCIPY_IMPL, c_str!("xirr.py"), c_str!("scipy_py_xirr"))
.unwrap();

let xirr = py_mod.getattr("xirr").unwrap();
let data = PaymentsLoader::from_csv(py, $file).to_columns();
b.iter(|| xirr.call1(black_box((data.1, data.0))).unwrap())
b.iter(|| xirr.call1(black_box((&data.1, &data.0))).unwrap())
});
}
};
Expand All @@ -81,12 +91,17 @@ macro_rules! bench_python {
#[bench]
fn $name(b: &mut Bencher) {
Python::with_gil(|py| {
let xirr = PyModule::from_code(py, PURE_PYTHON_IMPL, "xirr.py", "pure_py_xirr")
.unwrap()
.getattr("xirr")
.unwrap();
let py_mod = PyModule::from_code(
py,
PURE_PYTHON_IMPL,
c_str!("xirr.py"),
c_str!("pure_py_xirr"),
)
.unwrap();

let xirr = py_mod.getattr("xirr").unwrap();
let data = PaymentsLoader::from_csv(py, $file).to_records();
b.iter(|| xirr.call1(black_box((data,))).unwrap())
b.iter(|| xirr.call1(black_box((&data,))).unwrap())
});
}
};
Expand Down
8 changes: 4 additions & 4 deletions benches/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn bench_from_records(b: &mut Bencher) {
Python::with_gil(|py| {
let input = "tests/samples/random_100.csv";
let data = common::PaymentsLoader::from_csv(py, input).to_records();
b.iter(|| pyxirr_call_impl!(py, "xirr", (data,)).unwrap());
b.iter(|| pyxirr_call_impl!(py, "xirr", (&data,)).unwrap());
});
}

Expand All @@ -22,7 +22,7 @@ fn bench_from_columns(b: &mut Bencher) {
Python::with_gil(|py| {
let input = "tests/samples/random_100.csv";
let data = common::PaymentsLoader::from_csv(py, input).to_columns();
b.iter(|| pyxirr_call_impl!(py, "xirr", data).unwrap());
b.iter(|| pyxirr_call_impl!(py, "xirr", &data).unwrap());
});
}

Expand All @@ -31,7 +31,7 @@ fn bench_from_dict(b: &mut Bencher) {
Python::with_gil(|py| {
let input = "tests/samples/random_100.csv";
let data = common::PaymentsLoader::from_csv(py, input).to_dict();
b.iter(|| pyxirr_call_impl!(py, "xirr", (data,)).unwrap());
b.iter(|| pyxirr_call_impl!(py, "xirr", (&data,)).unwrap());
});
}

Expand All @@ -40,6 +40,6 @@ fn bench_from_pandas(b: &mut Bencher) {
Python::with_gil(|py| {
let input = "tests/samples/random_100.csv";
let data = common::pd_read_csv(py, input);
b.iter(|| pyxirr_call_impl!(py, "xirr", (data,)).unwrap());
b.iter(|| pyxirr_call_impl!(py, "xirr", (&data,)).unwrap());
});
}
32 changes: 16 additions & 16 deletions benches/npf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ static B_1: &[i32] = &[-100, 39, 59, 55, 20];
#[bench]
fn bench_1_irr(b: &mut Bencher) {
Python::with_gil(|py| {
let payments = PyList::new(py, B_1);
b.iter(|| black_box(pyxirr_call_impl!(py, "irr", (payments,)).unwrap()));
let payments = PyList::new(py, B_1).unwrap();
b.iter(|| black_box(pyxirr_call_impl!(py, "irr", (&payments,)).unwrap()));
});
}

#[bench]
fn bench_1_irr_npf(b: &mut Bencher) {
Python::with_gil(|py| {
let irr = py.import("numpy_financial").unwrap().getattr("irr").unwrap();
let payments = PyList::new(py, B_1);
b.iter(|| black_box(irr.call1((payments,)).unwrap()))
let payments = PyList::new(py, B_1).unwrap();
b.iter(|| black_box(irr.call1((&payments,)).unwrap()))
});
}

Expand Down Expand Up @@ -60,17 +60,17 @@ static B_2: &[f64] = &[
#[bench]
fn bench_2_irr(b: &mut Bencher) {
Python::with_gil(|py| {
let payments = PyList::new(py, B_2);
b.iter(|| black_box(pyxirr_call_impl!(py, "irr", (payments,)).unwrap()));
let payments = PyList::new(py, B_2).unwrap();
b.iter(|| black_box(pyxirr_call_impl!(py, "irr", (&payments,)).unwrap()));
});
}

#[bench]
fn bench_2_irr_npf(b: &mut Bencher) {
Python::with_gil(|py| {
let irr = py.import("numpy_financial").unwrap().getattr("irr").unwrap();
let payments = PyList::new(py, B_2);
b.iter(|| black_box(irr.call1((payments,)).unwrap()))
let payments = PyList::new(py, B_2).unwrap();
b.iter(|| black_box(irr.call1((&payments,)).unwrap()))
});
}

Expand All @@ -79,17 +79,17 @@ static B_3: &[f64] = &[10.0, 1.0, 2.0, -3.0, 4.0];
#[bench]
fn bench_3_irr_none(b: &mut Bencher) {
Python::with_gil(|py| {
let payments = PyList::new(py, B_3);
b.iter(|| black_box(pyxirr_call_impl!(py, "irr", (payments,)).unwrap()));
let payments = PyList::new(py, B_3).unwrap();
b.iter(|| black_box(pyxirr_call_impl!(py, "irr", (&payments,)).unwrap()));
});
}

#[bench]
fn bench_3_irr_none_npf(b: &mut Bencher) {
Python::with_gil(|py| {
let irr = py.import("numpy_financial").unwrap().getattr("irr").unwrap();
let payments = PyList::new(py, B_3);
b.iter(|| black_box(irr.call1((payments,)).unwrap()))
let payments = PyList::new(py, B_3).unwrap();
b.iter(|| black_box(irr.call1((&payments,)).unwrap()))
});
}

Expand All @@ -98,16 +98,16 @@ static B_4: &[i32] = &[-1000, 100, 250, 500, 500];
#[bench]
fn bench_4_mirr(b: &mut Bencher) {
Python::with_gil(|py| {
let values = PyList::new(py, B_4);
b.iter(|| black_box(pyxirr_call_impl!(py, "mirr", (values, 0.1, 0.1)).unwrap()))
let values = PyList::new(py, B_4).unwrap();
b.iter(|| black_box(pyxirr_call_impl!(py, "mirr", (&values, 0.1, 0.1)).unwrap()))
});
}

#[bench]
fn bench_4_mirr_npf(b: &mut Bencher) {
Python::with_gil(|py| {
let mirr = py.import("numpy_financial").unwrap().getattr("mirr").unwrap();
let values = PyList::new(py, B_4);
b.iter(|| black_box(mirr.call1((values, 0.1, 0.1)).unwrap()))
let values = PyList::new(py, B_4).unwrap();
b.iter(|| black_box(mirr.call1((&values, 0.1, 0.1)).unwrap()))
});
}
Loading