Skip to content

Instantly share code, notes, and snippets.

@Dapid
Created October 12, 2017 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dapid/ed23a1bb8e96782c0b698edecff14435 to your computer and use it in GitHub Desktop.
Save Dapid/ed23a1bb8e96782c0b698edecff14435 to your computer and use it in GitHub Desktop.
Profile results
$ kernprof -lv scratch_4.py
Line # Hits Time Per Hit % Time Line Contents
==============================================================
13 @profile
14 def run():
15 1 47 47.0 0.1 midpoints = k[:-1] + np.diff(k)/2
16 1 43 43.0 0.1 idx_aux = np.searchsorted(sortedA, midpoints)
17 1 1 1.0 0.0 idx = []
18 1 1 1.0 0.0 count = 0
19 1 16 16.0 0.0 final_indices = np.zeros(sortedA.shape, dtype=int)
20 1 0 0.0 0.0 old_obj = None
21 30 23 0.8 0.1 for obj in idx_aux:
22 29 17 0.6 0.0 if obj != old_obj:
23 29 14 0.5 0.0 idx.append((obj, count))
24 29 8 0.3 0.0 old_obj = obj
25 29 14 0.5 0.0 count += 1
26 1 1 1.0 0.0 old_idx = 0
27 30 20 0.7 0.0 for idx_A, idx_k in idx:
28 29 4824 166.3 11.4 final_indices[old_idx:idx_A] = idx_k
29 29 20 0.7 0.0 old_idx = idx_A
30
31 1 504 504.0 1.2 final_indices[old_idx:] = len(k)-1
32
33 1 36792 36792.0 86.9 indicesClosest = final_indices[inv_indices_sort]
import time
import numpy as np
A = np.random.random(3000000)
k = np.random.random(30)
indices_sort = np.argsort(A)
sortedA = A[indices_sort]
inv_indices_sort = np.argsort(indices_sort)
k.sort()
@profile # <---- Remove for running
def run():
midpoints = k[:-1] + np.diff(k)/2
idx_aux = np.searchsorted(sortedA, midpoints)
idx = []
count = 0
final_indices = np.zeros(sortedA.shape, dtype=int)
old_obj = None
for obj in idx_aux:
if obj != old_obj:
idx.append((obj, count))
old_obj = obj
count += 1
old_idx = 0
for idx_A, idx_k in idx:
final_indices[old_idx:idx_A] = idx_k
old_idx = idx_A
final_indices[old_idx:] = len(k)-1
indicesClosest = final_indices[inv_indices_sort]
t0 = time.time()
run()
print(time.time() - t0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment