Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you have hit a problem where a different method and some <em>a priori</em> knowledge of the array would really help. The kind of thing where you have a X probability of finding your answer in the first Y percent of the data. The splitting up the problem with the hope of getting lucky then doing this in python with a nested list comprehension or something. </p> <p>Writing a C function to do this brute force isn't too hard using <a href="http://docs.python.org/library/ctypes.html" rel="noreferrer">ctypes</a> either. </p> <p>The C code I hacked together (index.c):</p> <pre><code>long index(long val, long *data, long length){ long ans, i; for(i=0;i&lt;length;i++){ if (data[i] == val) return(i); } return(-999); } </code></pre> <p>and the python:</p> <pre><code># to compile (mac) # gcc -shared index.c -o index.dylib import ctypes lib = ctypes.CDLL('index.dylib') lib.index.restype = ctypes.c_long lib.index.argtypes = (ctypes.c_long, ctypes.POINTER(ctypes.c_long), ctypes.c_long) import numpy as np np.random.seed(8675309) a = np.random.random_integers(0, 100, 10000) print lib.index(57, a.ctypes.data_as(ctypes.POINTER(ctypes.c_long)), len(a)) </code></pre> <p>and I get 92.</p> <p>Wrap up the python into a proper function and there you go.</p> <p>The C version is a lot (~20x) faster for this seed (warning I am not good with timeit)</p> <pre><code>import timeit t = timeit.Timer('np.where(a==57)[0][0]', 'import numpy as np; np.random.seed(1); a = np.random.random_integers(0, 1000000, 10000000)') t.timeit(100)/100 # 0.09761879920959472 t2 = timeit.Timer('lib.index(57, a.ctypes.data_as(ctypes.POINTER(ctypes.c_long)), len(a))', 'import numpy as np; np.random.seed(1); a = np.random.random_integers(0, 1000000, 10000000); import ctypes; lib = ctypes.CDLL("index.dylib"); lib.index.restype = ctypes.c_long; lib.index.argtypes = (ctypes.c_long, ctypes.POINTER(ctypes.c_long), ctypes.c_long) ') t2.timeit(100)/100 # 0.005288000106811523 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload