Skip to content

Commit

Permalink
Merge pull request numpy#13530 from hameerabbasi/sort-bench-modify
Browse files Browse the repository at this point in the history
BENCH: Modify benchmarks for radix sort.
  • Loading branch information
seberg authored May 13, 2019
2 parents 4ad33d2 + 01eebb1 commit 577cd53
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions benchmarks/benchmarks/bench_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ def time_select_larger(self):
np.select(self.cond_large, ([self.d, self.e] * 10))


def memoize(f):
_memoized = {}
def wrapped(*args):
if args not in _memoized:
_memoized[args] = f(*args)

return _memoized[args].copy()

return f


class SortGenerator(object):
# The size of the unsorted area in the "random unsorted area"
# benchmarks
Expand All @@ -103,6 +114,7 @@ class SortGenerator(object):
BUBBLE_SIZE = 100

@staticmethod
@memoize
def random(size, dtype):
"""
Returns a randomly-shuffled array.
Expand All @@ -112,27 +124,31 @@ def random(size, dtype):
return arr

@staticmethod
@memoize
def ordered(size, dtype):
"""
Returns an ordered array.
"""
return np.arange(size, dtype=dtype)

@staticmethod
@memoize
def reversed(size, dtype):
"""
Returns an array that's in descending order.
"""
return np.arange(size-1, -1, -1, dtype=dtype)

@staticmethod
@memoize
def uniform(size, dtype):
"""
Returns an array that has the same value everywhere.
"""
return np.ones(size, dtype=dtype)

@staticmethod
@memoize
def swapped_pair(size, dtype, swap_frac):
"""
Returns an ordered array, but one that has ``swap_frac * size``
Expand All @@ -145,6 +161,7 @@ def swapped_pair(size, dtype, swap_frac):
return a

@staticmethod
@memoize
def sorted_block(size, dtype, block_size):
"""
Returns an array with blocks that are all sorted.
Expand All @@ -159,6 +176,7 @@ def sorted_block(size, dtype, block_size):
return np.array(b)

@classmethod
@memoize
def random_unsorted_area(cls, size, dtype, frac, area_size=None):
"""
This type of array has random unsorted areas such that they
Expand All @@ -176,6 +194,7 @@ def random_unsorted_area(cls, size, dtype, frac, area_size=None):
return a

@classmethod
@memoize
def random_bubble(cls, size, dtype, bubble_num, bubble_size=None):
"""
This type of array has ``bubble_num`` random unsorted areas.
Expand All @@ -197,7 +216,7 @@ class Sort(Benchmark):
# In NumPy 1.17 and newer, 'merge' can be one of several
# stable sorts, it isn't necessarily merge sort.
['quick', 'merge', 'heap'],
['float64', 'int64', 'uint64'],
['float64', 'int64', 'int16'],
[
('random',),
('ordered',),
Expand All @@ -206,15 +225,15 @@ class Sort(Benchmark):
('sorted_block', 10),
('sorted_block', 100),
('sorted_block', 1000),
('swapped_pair', 0.01),
('swapped_pair', 0.1),
('swapped_pair', 0.5),
('random_unsorted_area', 0.5),
('random_unsorted_area', 0.1),
('random_unsorted_area', 0.01),
('random_bubble', 1),
('random_bubble', 5),
('random_bubble', 10),
# ('swapped_pair', 0.01),
# ('swapped_pair', 0.1),
# ('swapped_pair', 0.5),
# ('random_unsorted_area', 0.5),
# ('random_unsorted_area', 0.1),
# ('random_unsorted_area', 0.01),
# ('random_bubble', 1),
# ('random_bubble', 5),
# ('random_bubble', 10),
],
]
param_names = ['kind', 'dtype', 'array_type']
Expand All @@ -227,10 +246,10 @@ def setup(self, kind, dtype, array_type):
array_class = array_type[0]
self.arr = getattr(SortGenerator, array_class)(self.ARRAY_SIZE, dtype, *array_type[1:])

def time_sort_inplace(self, kind, dtype, array_type):
self.arr.sort(kind=kind)

def time_sort(self, kind, dtype, array_type):
# Using np.sort(...) instead of arr.sort(...) because it makes a copy.
# This is important because the data is prepared once per benchmark, but
# used across multiple runs.
np.sort(self.arr, kind=kind)

def time_argsort(self, kind, dtype, array_type):
Expand Down

0 comments on commit 577cd53

Please sign in to comment.