aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/algorithms
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2016-06-25 20:02:43 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2016-06-25 20:02:43 +0300
commit87aa515305cfc620982fc10a4de420c2f25f1948 (patch)
tree2e16f8e68d7f836aad7d82193db6ee903121ff6b /algorithms
parentcode style (diff)
downloadsorting-algorithms-87aa515305cfc620982fc10a4de420c2f25f1948.tar.gz
sorting-algorithms-87aa515305cfc620982fc10a4de420c2f25f1948.zip
add heuristic to derive time units
Diffstat (limited to '')
-rw-r--r--algorithms/params.py42
1 files changed, 37 insertions, 5 deletions
diff --git a/algorithms/params.py b/algorithms/params.py
index cc35e33..50a190f 100644
--- a/algorithms/params.py
+++ b/algorithms/params.py
@@ -2,6 +2,7 @@
# This file is licensed under the terms of the MIT License.
# See LICENSE.txt for details.
+from enum import Enum
from numbers import Integral
from .inputgen import InputKind
@@ -9,6 +10,24 @@ from .plotter import PlotBuilder
from . import registry
from .timer import Timer
+class TimeUnits(Enum):
+ SECONDS = 'seconds'
+ MILLISECONDS = 'milliseconds'
+ MICROSECONDS = 'microseconds'
+
+ def get_factor(self):
+ if self is TimeUnits.SECONDS:
+ return 1.
+ elif self is TimeUnits.MILLISECONDS:
+ return 1000.
+ elif self is TimeUnits.MICROSECONDS:
+ return 1000000.
+ else:
+ raise NotImplementedError('invalid time units: ' + str(self))
+
+ def __str__(self):
+ return self.value
+
class AlgorithmParameters:
def __init__(self, algorithm, min_len, max_len,
input_kind=InputKind.AVERAGE, iterations=1):
@@ -86,8 +105,8 @@ class AlgorithmParameters:
return 'Input length'
@staticmethod
- def _format_plot_ylabel():
- return 'Running time (sec)'
+ def _format_plot_ylabel(units):
+ return 'Running time ({})'.format(units)
def _format_plot_title(self):
return '{}, {} case'.format(
@@ -96,14 +115,27 @@ class AlgorithmParameters:
def _format_plot_suptitle(self):
return self.algorithm.display_name
+ @staticmethod
+ def _derive_time_units(ys):
+ max_y = max(ys)
+ if max_y > 0.1:
+ return TimeUnits.SECONDS
+ elif max_y > 0.0001:
+ return TimeUnits.MILLISECONDS
+ else:
+ return TimeUnits.MICROSECONDS
+
def plot_running_time(self, output_path=None):
+ xs, ys = self.measure_running_time()
+ units = self._derive_time_units(ys)
+ ys = [y * units.get_factor() for y in ys]
+
plot_builder = PlotBuilder()
plot_builder.show_grid()
plot_builder.set_xlabel(self._format_plot_xlabel())
- plot_builder.set_ylabel(self._format_plot_ylabel())
- plot_builder.set_yticklabels_scientific()
+ plot_builder.set_ylabel(self._format_plot_ylabel(units))
+ #plot_builder.set_yticklabels_scientific()
plot_builder.set_title(self._format_plot_title())
- xs, ys = self.measure_running_time()
plot_builder.plot(xs, ys)
if output_path is None:
plot_builder.show()