Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by GiantDwarf01 · Nov 25, 2019 at 02:27 AM · optimizationloopspeed up

Speeding up nested for loop of structs and calculations

Hello, I have a project where I’m essentially comparing a set of points against itself to find the best pair of points. Each point has a “score” by calculating the number of points in a different set - let’s call them ground points and the original set wall points for simplicity - it can linecast to, however things get a bit more complex than just taking the top two scoring wall points because for each ground point that both wall points can see, the score for that particular point is dependent on the angle difference between the two line casts or rather the amount of coverage that point could see. For example: Wall point A can cast to ground point 1,2, and 3, Wall point B can see 2, 3, and 4, and Wall point C can see 1, 2, 3, and 4. The scoring for A alone would be 3*180, B would be 3*180, and C would be 4*180. However, let’s say B is on the opposite wall of A and C is on the neighboring wall. Then the score of the AB pair would be 3*180+3*180, whereas AC would get a score of 4*180 + 3*90.

I’ve gotten that all to work, however it currently is very slow as I have a for loop going through each wall point, with another for loop for each wall point, looping through the ground points each one can cast to, checking if it’s seen by both or just one of the points, then if it’s both calculating the angle offset, and finally adding the score for that pair. It works, but it’s slow as heck (I believe it might be O(n^2) or O(n^3)? It’s been a bit since I’ve used big-O notation ha). I was wondering if anyone may have some insight into how to either A) optimize my calculations, or B) get them to work without freezing up the program for such a long time.

Here’s an example of the code I’m using:

 for (int i = 0; i < wallSpots.Count; i++){
     for (int j = i + 1; j < wallSpots.Count; j++){
         int sharedPoints = 0
         for each (point in wallSpots[i].seenPoints){
             if (wallSpots[j].seenPoints.ContainsKey(point)){
                 sharedPoints++;
                 score += 180 + Vector3.Angle(wallSpots[i].seenPoints[point], wallSpots[j].seenPoints[point])
            }
         }
         score += 180 * ( (wallSpots[i].seenPoints.Count - sharedPoints) + (wallSpots[j].seenPoints.Count - sharedPoints) )
    }
  }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TreyH · Nov 25, 2019 at 03:47 AM 0
Share

A picture might help, btw. Also, do be careful as this isn't really a Unity question and folks can get testy about that.

How many elements are usually in wallSpots? Your bottleneck might be the number of things being considered and them all needing that angle calculation. I don't know much about your situation, but that iteration setup feels suspect. What does the angle do here, considering that those seem to be points and not directions?

avatar image GiantDwarf01 TreyH · Nov 25, 2019 at 05:29 AM 0
Share

There's usually at least 50 spots in wallSpots. I'm basically trying to deter$$anonymous$$e the best placement of a pair of VR sensor cameras in a room, so angle is to track if the cameras can see the player at any given angle at that point, avoiding dead spots if your back is towards one camera or something

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

122 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom particle system loop optimization 0 Answers

How do I optimize the "Player Loop"? 2 Answers

Need tips for optimizing main func that loops through thousands of particles 0 Answers

Managing a lot of GameObjects, calling functions on 1000 objects in a loop is very slow - Optimize/ECS/Delegates? 1 Answer

How to play many animations at once without slowdown? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges