Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

Python Hanging on Length and Split functions

I have a bit of code and it is hanging when I run it. I have narrowed it down to the following line. When I remove this line, my code does not hang.

However, there is nothing in this line which would make me think it could hang. There is no loops. Is there any known issues where string functions can hang given unknown input?

Here is the line causing the problem:

len(body.split("
")[-1]) >= 67

Here is the full function. Notice I have commented things out trying to narrow this down:

    def __str__(self):
        body = ""
        for i in range(0, self.height):
            for j in range (0, self.width):
                body += str(clamp(self.canvas[j][i]).red)
                if len(body.split("
")[-1]) >= 67 and j != self.width - 1:
                    body += "
"
                else:
                    body += " "
                #body += str(clamp(self.canvas[j][i]).green)
                #if len(body.split("
")[-1]) >= 67 and j != self.width - 1:
                #    body += "
"
                #else:
                #    body += " "
                #body += str(clamp(self.canvas[j][i]).blue)
                #if len(body.split("
")[-1]) >= 67 and j != self.width - 1:
                #    body += "
"
                #elif j != self.width - 1:
                #    body += " "
            if i != self.height - 1:
                body += "
"
        return body
question from:https://stackoverflow.com/questions/65643580/python-hanging-on-length-and-split-functions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

After adding some print statements printing i and j in the for loop, I found that it was running, but slowing down at an almost exponential speed as i increased. Because of this continued exponential slow down, it seemed as if it will never finish.

I rewrote the str function and sped up the total time to write_file() to 12 seconds for a canvas of size 900 by 550.

Here is the new speedier function:

    def __str__(self):
        body = ""
        for i in range(0, self.height):
            cur = ""
            for j in range (0, self.width):
                clamped = clamp(self.canvas[j][i])
                print(str(i) + " " + str(j))
                cur += str(clamped.red)
                if len(cur) >= 67 and j != self.width - 1:
                    cur += "
"
                    body += cur
                    cur = ""
                else:
                    cur += " "
                cur += str(clamped.green)
                if len(cur) >= 67 and j != self.width - 1:
                    cur += "
"
                    body += cur
                    cur = ""
                else:
                    cur += " "
                cur += str(clamped.blue)
                if len(cur) >= 67 and j != self.width - 1:
                    cur += "
"
                    body += cur
                    cur = ""
                elif j != self.width - 1:
                    cur += " "
            body += cur
            if i != self.height - 1:
                body += "
"
        return body

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...