Comments (2)
find_index is constant time; searchsorted is logarithmic. Big enough
difference to be worth maintaining? Maybe not.
On Fri, Feb 5, 2016 at 10:01 AM, giumas [email protected] wrote:
The function find_index (
https://github.com/AllenDowney/ThinkDSP/blob/master/code/thinkdsp.py#L143)
does the same as numpy.searchsorted (
http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.searchsorted.html)
but in a less efficient way.I cannot find a reason to maintain find_index. Do you?
—
Reply to this email directly or view it on GitHub
#18.
from thinkdsp.
Did you try to time it?
In [1]:
import numpy as np
def find_index(x, xs):
"""Find the index corresponding to a given value in an array."""
n = len(xs)
start = xs[0]
end = xs[-1]
i = round((n-1) * (x - start) / (end - start))
return int(i)
test_array = np.array(range(99999))/1.0
print(test_array)
[ 0.00000000e+00 1.00000000e+00 2.00000000e+00 ..., 9.99960000e+04
9.99970000e+04 9.99980000e+04]
In [2]:
%timeit find_index(50000, test_array)
The slowest run took 8.06 times longer than the fastest. This could mean that an intermediate result is being cached
1000000 loops, best of 3: 1.16 µs per loop
In [3]:
%timeit test_array.searchsorted(50000)
The slowest run took 25.84 times longer than the fastest. This could mean that an intermediate result is being cached
1000000 loops, best of 3: 502 ns per loop
In [4]:
%timeit find_index(0, test_array)
The slowest run took 9.03 times longer than the fastest. This could mean that an intermediate result is being cached
1000000 loops, best of 3: 1.14 µs per loop
In [5]:
%timeit test_array.searchsorted(0)
The slowest run took 19.14 times longer than the fastest. This could mean that an intermediate result is being cached
1000000 loops, best of 3: 505 ns per loop
In [6]:
%timeit find_index(1000000, test_array)
The slowest run took 9.18 times longer than the fastest. This could mean that an intermediate result is being cached
1000000 loops, best of 3: 1.18 µs per loop
In [7]:
%timeit test_array.searchsorted(1000000)
The slowest run took 16.96 times longer than the fastest. This could mean that an intermediate result is being cached
1000000 loops, best of 3: 516 ns per loop
There could be some optimization in numpy.searchsorted and less overhead.. just a guess.
from thinkdsp.
Related Issues (20)
- "No handles with labels found to put in legend" issue HOT 8
- 2 small issues I noticed (inverted lines of code in book + pcolor graph doesn't show) HOT 4
- Question about decaying noise generation HOT 1
- Anaconda distro link update HOT 1
- Playback audio HOT 5
- Input 18 of the code/voss.ipynb, the Barlett Method function, fails with a type error HOT 1
- TypeError at use of make_spectrogram HOT 3
- How can I read from input audio devide and find decible value ?
- Book References HOT 1
- correlation wave after use shift in time HOT 1
- Correction on Chapter 5 test functions HOT 1
- fb.csv and FB.csv lead to duplicate file error on windows and modified state of fb.csv right after cloning. HOT 3
- Spectrum convolve issue - even number of samples HOT 1
- fractions.gcd() is replaced by math.gcd() in python3.5 HOT 1
- if read_wave() then write(), the new audio is different in amp. HOT 1
- Corrections from Luís Guimarães HOT 1
- Github ,need to upgrade me HOT 1
- How to apply SNR to Guassian White Noise? HOT 3
- Installation of thinkdsp HOT 7
- AttributeError exception being thrown in thinkdsp.py call to numpy HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from thinkdsp.