I'm having trouble adding in Python. I am writing an algorithm that checks for the presence of two overlapping circles in a (large) set of circles. I start by placing the extreme points of the circles (x_i-R_i and x_i + R_i) in the list, and then sorting the list.
class Circle: def __init__(self, middle, radius): self.m = middle self.r = radius
In the span, I generate N random circles and put them on the list of circles.
""" Makes a list with all the extreme points of the circles. Format = [Extreme, left/right ~ 0/1 extreme, index] Seperate function for performance reason, python handles local variables faster. Garbage collect is temporarily disabled since a bug in Python makes list.append run in O(n) time instead of O(1) """ def makeList(): """gc.disable()""" list = [] append = list.append for circle in circles: append([circle.m[0]-circle.r, 0, circles.index(circle)]) append([circle.m[0] + circle.r, 1, circles.index(circle)]) """gc.enable()""" return list
When doing this with 50k circles, it takes more than 75 seconds to create a list. As you can see in the comments, I wrote that I turned off garbage collection, put it in a separate function, used
append = list.append append(foo)
instead
list.append(foo)
I turned off gc, as after some searching, it seems that there is an error starting python so that the application runs in O (n) instead of O (c).
So is this the fastest way or is there a way to make this run faster? Any help is appreciated.
source share