-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
initial stab at the numba.typed.List benchmarks #10
base: master
Are you sure you want to change the base?
Changes from 15 commits
75af513
d0558e5
c997b5f
5eeeb6c
7234402
255a119
5d0fce2
8732ef8
cbf3e8c
ca0011f
921321b
6957bbc
27fb3d8
db6103e
fb90310
a6be44c
5c870d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -25,3 +25,33 @@ cd numba-benchmark | |||||
```bash | ||||||
asv run | ||||||
``` | ||||||
|
||||||
## Comparing two commits | ||||||
|
||||||
Run `asv` on the first commit: | ||||||
|
||||||
```bash | ||||||
asv run "-1 abcdefg | ||||||
``` | ||||||
|
||||||
Run `asv` on the second commit: | ||||||
|
||||||
```bash | ||||||
asv run "-1 1235567 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed in 5c870d3 |
||||||
``` | ||||||
|
||||||
Compare them: | ||||||
|
||||||
```bash | ||||||
asv compare "abcdefg" "1234567" | ||||||
``` | ||||||
|
||||||
Useful options to consider: | ||||||
|
||||||
```bash | ||||||
asv run --verbose --show-stderr -b 'bench_typed_list' "-1 abcdefg" | ||||||
``` | ||||||
|
||||||
```bash | ||||||
asv compare --machine machine.local "abcdefg" "1234567" | ||||||
``` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add 1 line to say what they do/why they are useful? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 5c870d3 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,294 @@ | ||
""" | ||
Benchmarks for numba.typed.List | ||
""" | ||
|
||
import numpy as np | ||
from numba import njit | ||
from numba.typed import List | ||
from numba.typed.typedlist import _sort | ||
from numba.core.registry import dispatcher_registry | ||
from numba.core.typing import Signature | ||
from numba.core.types import ListType, int64, float64, none, boolean, Array | ||
|
||
SIZE = 10**5 | ||
SEED = 23 | ||
|
||
def check_getitem_unchecked(): | ||
|
||
@njit | ||
def use_getitem_unchecked(): | ||
tl = List((1, 2, 3, 4)) | ||
return tl.getitem_unchecked(0) | ||
|
||
try: | ||
use_getitem_unchecked() | ||
except Exception: | ||
return False | ||
else: | ||
return True | ||
|
||
|
||
have_getitem_unchecked = check_getitem_unchecked() | ||
|
||
|
||
@njit | ||
def make_random_typed_list_int(n): | ||
tl = List() | ||
np.random.seed(SEED) | ||
for i in range(n): | ||
tl.append(np.random.randint(0, 100)) | ||
return tl | ||
|
||
@njit | ||
def make_random_typed_list_float(n): | ||
tl = List() | ||
np.random.seed(SEED) | ||
for i in range(n): | ||
tl.append(np.random.randn()) | ||
return tl | ||
|
||
@njit | ||
def make_random_typed_list_array(n): | ||
tl = List() | ||
np.random.seed(SEED) | ||
for i in range(n): | ||
tl.append(np.zeros(4,)) | ||
return tl | ||
|
||
def make_random_python_list(n): | ||
pl = list() | ||
np.random.seed(SEED) | ||
for i in range(n): | ||
pl.append(np.random.randint(0, 100)) | ||
return pl | ||
|
||
|
||
def clear_dispatcher(dispatcher): | ||
dispatcher._make_finalizer()() | ||
dispatcher._reset_overloads() | ||
dispatcher._cache.flush() | ||
dispatcher._can_compile = True | ||
|
||
|
||
class BaseSuite: | ||
min_run_count = 5 | ||
warmup_time = 0.0 | ||
number = 1 | ||
repeat = 5 | ||
|
||
|
||
class SortSuite(BaseSuite): | ||
|
||
def setup(self): | ||
self.tl = make_random_typed_list_int(SIZE) | ||
self.tl.sort() | ||
self.dispatcher = dispatcher_registry['cpu'](_sort.py_func) | ||
self.signature = Signature(none, | ||
[ListType(int64), none, boolean], | ||
None) | ||
clear_dispatcher(self.dispatcher) | ||
|
||
def time_execute_sort(self): | ||
self.tl.sort() | ||
|
||
def time_compile_sort(self): | ||
self.dispatcher.compile(self.signature) | ||
|
||
|
||
class ConstructionSuite(BaseSuite): | ||
|
||
def setup(self): | ||
self.pl = make_random_python_list(SIZE) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it should seed the the rng There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
List(self.pl) | ||
|
||
def time_construct_from_python_list(self): | ||
List(self.pl) | ||
|
||
def time_construct_in_njit_function(self): | ||
make_random_typed_list_int(SIZE) | ||
|
||
|
||
class ReductionSuite(BaseSuite): | ||
|
||
def setup(self): | ||
raise NotImplementedError | ||
|
||
def post_setup(self): | ||
|
||
self.reduction_sum = self.define_function() | ||
|
||
self.reduction_sum_fastmath = njit(self.reduction_sum, fastmath=True) | ||
self.reduction_sum_no_fastmath = njit(self.reduction_sum) | ||
|
||
self.reduction_sum_fastmath(self.tl) | ||
self.reduction_sum_no_fastmath(self.tl) | ||
|
||
self.fastmath_dispatcher = dispatcher_registry['cpu']( | ||
self.reduction_sum_fastmath.py_func) | ||
self.fastmath_dispatcher.targetoptions['fastmath'] = True | ||
|
||
self.no_fastmath_dispatcher = dispatcher_registry['cpu']( | ||
self.reduction_sum_no_fastmath.py_func) | ||
|
||
|
||
clear_dispatcher(self.fastmath_dispatcher) | ||
clear_dispatcher(self.no_fastmath_dispatcher) | ||
|
||
def time_execute_reduction_sum_fastmath(self): | ||
self.reduction_sum_fastmath(self.tl) | ||
|
||
def time_compile_reduction_sum_fastmath(self): | ||
self.fastmath_dispatcher.compile(self.signature) | ||
|
||
def time_execute_reduction_sum_no_fastmath(self): | ||
self.reduction_sum_no_fastmath(self.tl) | ||
|
||
def time_compile_reduction_sum_no_fastmath(self): | ||
self.no_fastmath_dispatcher.compile(self.signature) | ||
|
||
|
||
class IteratorReductionSuiteInt(ReductionSuite): | ||
|
||
def setup(self): | ||
|
||
self.tl = make_random_typed_list_int(SIZE) | ||
self.signature = Signature(int64, [ListType(int64)], None) | ||
self.post_setup() | ||
|
||
def define_function(self): | ||
|
||
def reduction_sum(tl): | ||
agg = 0 | ||
for i in tl: | ||
agg += i | ||
return agg | ||
|
||
return reduction_sum | ||
|
||
class IteratorReductionSuiteFloat(ReductionSuite): | ||
|
||
def setup(self): | ||
|
||
self.tl = make_random_typed_list_float(SIZE) | ||
self.signature = Signature(float64, [ListType(float64)], None) | ||
self.post_setup() | ||
|
||
def define_function(self): | ||
|
||
def reduction_sum(tl): | ||
agg = 0.0 | ||
for i in tl: | ||
agg += i | ||
return agg | ||
|
||
return reduction_sum | ||
|
||
class ForLoopReductionSuiteInt(ReductionSuite): | ||
|
||
def setup(self): | ||
|
||
self.tl = make_random_typed_list_int(SIZE) | ||
self.signature = Signature(int64, [ListType(int64)], None) | ||
self.post_setup() | ||
|
||
def define_function(self): | ||
|
||
def reduction_sum(tl): | ||
agg = 0 | ||
length = len(tl) | ||
for i in range(length): | ||
agg += tl[i] | ||
return agg | ||
|
||
return reduction_sum | ||
|
||
class ForLoopReductionSuiteFloat(ReductionSuite): | ||
|
||
def setup(self): | ||
|
||
self.tl = make_random_typed_list_float(SIZE) | ||
self.signature = Signature(float64, [ListType(float64)], None) | ||
self.post_setup() | ||
|
||
def define_function(self): | ||
|
||
def reduction_sum(tl): | ||
agg = 0.0 | ||
length = len(tl) | ||
for i in range(length): | ||
agg += tl[i] | ||
return agg | ||
|
||
return reduction_sum | ||
|
||
|
||
class GetitemUncheckedReductionSuiteInt(ReductionSuite): | ||
|
||
def setup(self): | ||
|
||
self.tl = make_random_typed_list_int(SIZE) | ||
self.signature = Signature(int64, [ListType(int64)], None) | ||
self.post_setup() | ||
|
||
def define_function(self): | ||
|
||
def reduction_sum_regular(tl): | ||
agg = 0 | ||
length = len(tl) | ||
for i in range(length): | ||
agg += tl[i] | ||
return agg | ||
|
||
def reduction_sum_unchecked(tl): | ||
agg = 0 | ||
length = len(tl) | ||
for i in range(length): | ||
agg += tl.getitem_unchecked(i) | ||
return agg | ||
|
||
return reduction_sum_unchecked if have_getitem_unchecked else reduction_sum_regular | ||
|
||
class GetitemUncheckedReductionSuiteFloat(ReductionSuite): | ||
|
||
def setup(self): | ||
|
||
self.tl = make_random_typed_list_float(SIZE) | ||
self.signature = Signature(float64, [ListType(float64)], None) | ||
self.post_setup() | ||
|
||
def define_function(self): | ||
|
||
def reduction_sum_regular(tl): | ||
agg = 0.0 | ||
length = len(tl) | ||
for i in range(length): | ||
agg += tl[i] | ||
return agg | ||
|
||
def reduction_sum_unchecked(tl): | ||
agg = 0.0 | ||
length = len(tl) | ||
for i in range(length): | ||
agg += tl.getitem_unchecked(i) | ||
return agg | ||
|
||
return reduction_sum_unchecked if have_getitem_unchecked else reduction_sum_regular | ||
|
||
|
||
class ArrayListSuite(ReductionSuite): | ||
|
||
def setup(self): | ||
|
||
self.tl = make_random_typed_list_array(SIZE) | ||
self.signature = Signature(float64, [ListType(Array(float64, 1, 'C'))], None) | ||
self.post_setup() | ||
|
||
def define_function(self): | ||
|
||
def array_reduction(tl): | ||
agg = 0.0 | ||
for i in tl: | ||
agg += i.sum() | ||
return agg | ||
|
||
return array_reduction |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, that's hexadecimal? Why's there a
g
in it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessarily, it can be any commit-ish - which includes tags and branches. Though I understand that this is misleading because it looks like hex.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 5c870d3