-
-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Amend doc, add is_iterable func, compute machine eps dynamically
- Loading branch information
1 parent
4029c77
commit 55fc876
Showing
7 changed files
with
135 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Benchmark various approaches to functions to speed things up. | ||
# ... hopefully. | ||
|
||
from __future__ import absolute_import | ||
|
||
import numpy as np | ||
|
||
import time | ||
|
||
|
||
def _do_time(func, n_iter=10, *args, **kwargs): | ||
times = [] | ||
for _ in range(n_iter): | ||
start = time.time() | ||
func(*args, **kwargs) | ||
times.append(time.time() - start) | ||
|
||
times = np.asarray(times) | ||
print("Completed %i iterations (avg=%.6f, min=%.6f, max=%.6f)" | ||
% (n_iter, times.mean(), times.min(), times.max())) | ||
|
||
|
||
def benchmark_is_constant(): | ||
"""This benchmarks the "is_constant" function from ``pyramid.arima.utils``. | ||
This was added in 0.6.2. | ||
""" | ||
# WINNER! | ||
def is_const1(x): | ||
"""This is the version in Pyramid 0.6.2. | ||
Parameters | ||
---------- | ||
x : np.ndarray | ||
This is the array. | ||
""" | ||
return (x == x[0]).all() | ||
|
||
def is_const2(x): | ||
"""This should ostensibly only take O(N) rather than O(2N) like | ||
its predecessor. But we'll see... | ||
Parameters | ||
---------- | ||
x : np.ndarray | ||
This is the array. | ||
""" | ||
return np.unique(x).shape[0] == 1 | ||
|
||
x = np.random.choice(np.arange(10), 1000000, replace=True) | ||
_do_time(is_const1, 25, x) | ||
_do_time(is_const2, 25, x) | ||
|
||
|
||
if __name__ == '__main__': | ||
benchmark_is_constant() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters