From c36d9bf54346cdb135bb0f580891fcddd60a8ea8 Mon Sep 17 00:00:00 2001 From: Marko Toplak Date: Thu, 2 Apr 2020 10:43:32 +0200 Subject: [PATCH] benchmarks for printing and saving tables --- benchmark/bench_save.py | 61 +++++++++++++++++++++++++++++++++++++ benchmark/bench_variable.py | 19 ++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 benchmark/bench_save.py create mode 100644 benchmark/bench_variable.py diff --git a/benchmark/bench_save.py b/benchmark/bench_save.py new file mode 100644 index 00000000000..74d6d35f563 --- /dev/null +++ b/benchmark/bench_save.py @@ -0,0 +1,61 @@ +from functools import partial +import os + +import numpy as np +import scipy.sparse + +from Orange.data import Table, ContinuousVariable, Domain +from .base import Benchmark, benchmark + + +def save(table, fn): + try: + table.save(fn) + finally: + os.remove(fn) + + +class BenchSave(Benchmark): + + def setup_dense(self, rows, cols, varkwargs=None): + if varkwargs is None: + varkwargs = {} + self.table = Table.from_numpy( # pylint: disable=W0201 + Domain([ContinuousVariable(str(i), **varkwargs) for i in range(cols)]), + np.random.RandomState(0).rand(rows, cols)) + + def setup_sparse(self, rows, cols, varkwargs=None): + if varkwargs is None: + varkwargs = {} + sparse = scipy.sparse.rand(rows, cols, density=0.01, format='csr', random_state=0) + self.table = Table.from_numpy( # pylint: disable=W0201 + Domain([ContinuousVariable(str(i), sparse=True, **varkwargs) for i in range(cols)]), + sparse) + + @benchmark(setup=partial(setup_dense, rows=100, cols=10)) + def bench_print_dense(self): + str(self.table) + + @benchmark(setup=partial(setup_dense, rows=100, cols=10, + varkwargs={"number_of_decimals": 2})) + def bench_print_dense_decimals(self): + str(self.table) + + @benchmark(setup=partial(setup_sparse, rows=100, cols=10), number=5) + def bench_print_sparse(self): + str(self.table) + + @benchmark(setup=partial(setup_sparse, rows=100, cols=10, + varkwargs={"number_of_decimals": 2}), + number=5) + def bench_print_sparse_decimals(self): + str(self.table) + + @benchmark(setup=partial(setup_dense, rows=100, cols=100)) + def bench_save_tab(self): + save(self.table, "temp_save.tab") + + @benchmark(setup=partial(setup_dense, rows=100, cols=100, + varkwargs={"number_of_decimals": 2})) + def bench_save_tab_decimals(self): + save(self.table, "temp_save.tab") diff --git a/benchmark/bench_variable.py b/benchmark/bench_variable.py new file mode 100644 index 00000000000..e81c54d1791 --- /dev/null +++ b/benchmark/bench_variable.py @@ -0,0 +1,19 @@ +from Orange.data import ContinuousVariable +from .base import Benchmark, benchmark + + +class BenchContinuous(Benchmark): + + # pylint: disable=no-self-use + @benchmark() + def bench_str_val_decimals(self): + a = ContinuousVariable("a", 4) + for _ in range(1000): + a.str_val(1.23456) + + # pylint: disable=no-self-use + @benchmark() + def bench_str_val_g(self): + a = ContinuousVariable("a") + for _ in range(1000): + a.str_val(1.23456)