Skip to content

Commit 4afacd4

Browse files
committed
BENCH: NPV: Actually create decimal arrays in benchmarks
This fixes a bug where we weren't creating decimal arrays in the benchmark. Instead of creating an array and setting the dtype we manually make a list of Decimals and create an array from that list.
1 parent b8592cf commit 4afacd4

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

benchmarks/benchmarks.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
import numpy_financial as npf
66

77

8+
def _to_decimal_array_1d(array):
9+
return np.array([Decimal(x) for x in array.tolist()])
10+
11+
12+
def _to_decimal_array_2d(array):
13+
decimals = [Decimal(x) for row in array.tolist() for x in row]
14+
return np.array(decimals).reshape(array.shape)
15+
16+
817
class Npv2D:
918

1019
param_names = ["n_cashflows", "cashflow_lengths", "rates_lengths"]
@@ -25,8 +34,8 @@ def setup(self, n_cashflows, cashflow_lengths, rates_lengths):
2534
cf_shape = (n_cashflows, cashflow_lengths)
2635
self.cashflows = rng.standard_normal(cf_shape)
2736
self.rates = rng.standard_normal(rates_lengths)
28-
self.cashflows_decimal = rng.standard_normal(cf_shape).astype(Decimal)
29-
self.rates_decimal = rng.standard_normal(rates_lengths).astype(Decimal)
37+
self.cashflows_decimal = _to_decimal_array_2d(self.cashflows)
38+
self.rates_decimal = _to_decimal_array_1d(self.rates)
3039

3140
def time_broadcast(self, n_cashflows, cashflow_lengths, rates_lengths):
3241
npf.npv(self.rates, self.cashflows)

0 commit comments

Comments
 (0)