Python Forum
How can I make a faster search algorithm
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I make a faster search algorithm
#15
In my code:
  • vect4 is the pattern
  • in the loop, I'm extracting parts of vect3 having the size of the pattern, and then I'm comparing it to vect4 (True or False)
  • at the same time the "check" vector (composed of booleans) is updated
  • At the end, I'm looking for "True" values in order to get both the position and the occurence(s) of the pattern

Some remarks:
  1. I've not been able to avoid the loop
  2. it's true in Python but not only: avoid (memory) dynamic allocation to speed up your code
  3. I guess we can adapt it for

hope it helps

I noticed some mistake in my prevous code; please consider the following one that is looking for a pattern of 1000 components in a vector of 3 million lines (it took 62 sec on my old laptop)
import numpy as np
import time, os, re

n = 1000;
vect3 = np.random.randint(2,size = 3000*n);
vect4 = np.random.randint(2,size = n);
## 4 occurences are manually created
vect3[1:1+n] = vect4;
vect3[6596:6596+n] = vect4;
vect3[10023:10023+n] = vect4;
vect3[569872:569872+n] = vect4;

## initialization / No match by default
check = np.full(3000*n, False, dtype = bool);

## partially vectorized
t0 = time.time()
for i in range(3000*n):
    check[i] = np.array_equiv(vect3[i:n+i],vect4);

sol = np.where(check == True); ## we're lokking for True occurences and its positions (see Tuple)
if (sol == []):
    print("No match")
else:
    l = np.size(sol);
    print("There are %d matche(s)" %l)
    print("Positions: %s" %sol)

t1 = time.time()    
print("Duration method: ", t1-t0)
Reply


Messages In This Thread
RE: How can I make a faster search algorithm - by paul18fr - Apr-15-2019, 08:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic binary search algorithm - using a while loop Drone4four 1 442 Jan-22-2024, 06:34 PM
Last Post: deanhystad
  Writing a Linear Search algorithm - malformed string representation Drone4four 10 1,126 Jan-10-2024, 08:39 AM
Last Post: gulshan212
  go over and search in numpy array faster caro 7 1,886 Jun-20-2022, 04:54 PM
Last Post: deanhystad
  Trying to search and make new column from the keyword. dgarg 1 1,514 Dec-20-2021, 08:41 PM
Last Post: deanhystad
  Search faster? Pedroski55 1 1,989 Dec-06-2020, 10:03 PM
Last Post: Larz60+
  how to make iterative search more efficient renergy 2 2,298 Jan-03-2020, 03:43 PM
Last Post: stullis
  To make an algorithm work faster pianistseb 3 2,849 Apr-01-2019, 08:42 AM
Last Post: Gribouillis
  How can I make this function faster? Brennan 10 6,317 Jun-29-2018, 08:33 PM
Last Post: ichabod801
  How do I make this code run faster? DontHurtMe 0 2,457 Nov-04-2017, 12:12 PM
Last Post: DontHurtMe

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020