Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 how can I improve the code to get it faster?
Hi guys,

my script must read a text file and produce an output. it works quite well but a piece of his code is very slow (it because of some instructions at the beginning of my scrtipt). they are like the example below and take around 30 seconds to read a text file composed by around 300000 lines:

x = ""
y = ""

for line in text:
    list = line.split()
    if "xxxx" in line and "t" in list[5] and "z" not in list[5]
        x = x + line
    elif "xxxx" in line and "a" not in list[7] and "b" not in list[8] and " hola " not in line:
        y = y + line

when I have many "not in" or "in" instructions for the same string, I tried to use this custom function, but unfortunately it doesn't solve the time issue:
def find_all_strings(text, strings_to_find, in_or_not_in):
    if in_or_not_in == "in":
        if all (string in text for string in strings_to_find):
            return True
            return False
    elif in_or_not_in == "not_in":
        if all (string not in text for string in strings_to_find):
            return True
            return False
is there a way to improve the code? my goal is implement a kind of "grep" and "grep -v" commands in the text document to take from it different configuration lines.
Adding many pieces to a string as in x = x + line is slow. It is much faster to append to a list like so
x = []

x = ''.join(x) # <-- join the pieces at the end.
aquerci likes this post
thanks Gribouillis!! it works! the study books don't teach you this powerful and simple concepts. thank you again!

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How can I improve this piece of code? aquerci 3 266 Nov-17-2019, 10:57 AM
Last Post: Gribouillis
  Help improve code efficiency benbrown03 9 578 Feb-20-2019, 03:45 PM
Last Post: ichabod801
  How to improve the quality of my code? grobattac37 3 464 Jan-25-2019, 06:17 PM
Last Post: ichabod801
  Another working code, help required for faster multithreading execution anna 0 853 Feb-09-2018, 03:26 AM
Last Post: anna
  Help required for faster execution of working code anna 2 1,007 Feb-09-2018, 03:00 AM
Last Post: anna
  How do I make this code run faster? DontHurtMe 0 938 Nov-04-2017, 12:12 PM
Last Post: DontHurtMe

Forum Jump:

Users browsing this thread: 1 Guest(s)