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
#7
Look at the following code:

# big_Erdos1=list(".... long string....
import numpy as np
def one():
    big_Erdos2=""
    for i in range(0,len(big_Erdos1)):
        if big_Erdos1[i]=="0":
            big_Erdos2+="1"
        else:
            big_Erdos2+="0"
    return big_Erdos2


res = np.array(["0"]*len(big_Erdos1))
data = np.array(big_Erdos1)  
def two():
    res[ data == "0"] = "1"
    return res.tolist()
If you measure time of execution for one() and two(), you get result like this:

Output:
> %timeit -n 1000 one() 258 µs ± 5.78 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) > %timeit -n 1000 two() 49.3 µs ± 2.93 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
The second solution is about 5x times faster than previous. This is because I used numpy
and assigned values to the array without any loops, but using vectorized notation. Numpy is implemented
in C and such operations executed much faster, than they would implemented as raw python loops.

So, you need to rewrite your data processing algorithm using vectorized notation of numpy (avoiding loops), or, if you need to find some substring in another string, using regular expressions (re module).
Reply


Messages In This Thread
RE: How can I make a faster search algorithm - by scidam - Apr-15-2019, 10:05 AM

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