chaimleib / intervaltree

A mutable, self-balancing interval tree. Queries may be by point, by range overlap, or by range containment.
Apache License 2.0
632 stars 106 forks source link

Fast way to build large tree? #102

Open 25thbamofthetower opened 4 years ago

25thbamofthetower commented 4 years ago

Hi,

I'm trying to build a tree that can be 6000+ nodes big, and it seems to take roughly 4 minutes. I was wondering if there's a fast way to do it?

tree = IntervalTree()
for obj in obj_list:
    tree.add(
        Interval(
            begin=obj.lo_location,
            end=obj.hi_location,
            data=obj,
    ),
)
chaimleib commented 3 years ago

Is the speed better if you do this?

tree = IntervalTree(Interval(obj.lo_location, obj.hi_location, obj) for obj in obj_list)
didimelli commented 3 years ago

Hello,

I am building trees that are not as big as @25thbamofthetower's (mine are around 100 items long), and I have tested 3 ways to build the tree:

  1. Dict-like insertion -> The slowest
    t = IntervalTree()
    for obj in obj_list:
    t[obj.begin : obj.end] = obj
  2. Using from_tuples classmethod -> Much better
  3. Using the approach you suggested in the previous comment to the issue -> Slightly better than 2