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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…