Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9246487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:30:03+00:00 2026-06-18T09:30:03+00:00

This check for if some points are inside a rectangle, and whenever it’s run

  • 0

This check for if some points are inside a rectangle, and whenever it’s run it slows my program down a lot. How can we change it to make it more efficient?

def draw_grid(self, box):
    for element in self.map_layout.all_map_objects:
        if element not in self.build_grid and box.area.collidepoint(element.checkpoint):
            self.build_grid.append(element)
        elif not box.area.collidepoint(element.checkpoint):
            if element in self.build_grid:
                self.build_grid.remove(element)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:30:05+00:00Added an answer on June 18, 2026 at 9:30 am

    One change which is trivial to make is to store the result of box.area.collidepoint:

    def draw_grid(self, box):
        for element in self.map_layout.all_map_objects:
            collidepoint = box.area.collidepoint(element.checkpoint)
            if element not in self.build_grid and collidepoint:
                self.build_grid.append(element)
            elif not collidepoint:
                if element in self.build_grid:
                    self.build_grid.remove(element)
    

    Other changes rely on the data structure that you need. For example, self.build_grid seems to be a list. __contains__ (in operator) on a list is an O(N) operation on average whereas if you could get by with a set, it would be O(1)!. The same thing goes with list.remove — Here you could use a try-except clause to remove one O(N) operation from the tight loop which might help if the element is usually in the list:

            elif not collidepoint:
                try:
                    self.build_grid.remove(element)
                except ValueError:
                    pass
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Guys check this website on FF and Chrome For some reason(s), jQuery isn't working
there is this check-in-out program here at my workplace, it only takes the data
I have some code for you guru's to check. I know this is probably
This there a way to remove this check, when a user first accesses my
I have just seen this // Check to see if the request is a
Hello I'm having an issue with this check box : <input type=checkbox id=hideTempSeries checked=checked
This question Check all associations before destroy in rails suggests using :dependent => :restrict
Check this code : HTML : <div style="position: absolute; visibility: visible; width: 172px;"> <img
Check this page. On either firefox, chrome, or IE9 / 10 the tabs on
This is the URL: Check this URL In that there is a red ribbon

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.