Finds a Character based on distance and health Announcing the arrival of Valued Associate...

Do square wave exist?

What would be the ideal power source for a cybernetic eye?

How do pianists reach extremely loud dynamics?

What's the meaning of "fortified infraction restraint"?

Is it ethical to give a final exam after the professor has quit before teaching the remaining chapters of the course?

Is there a kind of relay only consumes power when switching?

An adverb for when you're not exaggerating

If my PI received research grants from a company to be able to pay my postdoc salary, did I have a potential conflict interest too?

Withdrew £2800, but only £2000 shows as withdrawn on online banking; what are my obligations?

Do I really need recursive chmod to restrict access to a folder?

When a candle burns, why does the top of wick glow if bottom of flame is hottest?

What do you call a floor made of glass so you can see through the floor?

Did MS DOS itself ever use blinking text?

Why wasn't DOSKEY integrated with COMMAND.COM?

How can I use the Python library networkx from Mathematica?

8 Prisoners wearing hats

Wu formula for manifolds with boundary

2001: A Space Odyssey's use of the song "Daisy Bell" (Bicycle Built for Two); life imitates art or vice-versa?

What is the longest distance a player character can jump in one leap?

How to answer "Have you ever been terminated?"

Why didn't Eitri join the fight?

Maximum summed powersets with non-adjacent items

Is it a good idea to use CNN to classify 1D signal?

Using audio cues to encourage good posture



Finds a Character based on distance and health



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0












$begingroup$


I need to improve the efficiency of my program and, using the profiler, have narrowed the problem down to 2 key areas, but I am having trouble coming up with ways to make the program run better.



Based on my profiler's report, it seems to be telling me that my if functions are inefficient. Whats a better way to achieve a better result?



Character* FindAttackTarget() const
{
float weakestHp = FLT_MAX;
Character* weakestEnemy = nullptr;
uint64_t weakestCharId = INT64_MAX;

//Only attack characters that are within attack range
auto& gameChars = m_pGame->m_gameCharacters;


for (size_t i = 0; i < gameChars.size(); ++i)
{
auto& c = gameChars[i];
if (Location.Dist(c.GetLocation()) <= AttackRange &&
c.HP > 0 &&
c.Team != Team)
{
//We want the weakest with the lowest ID number - this keeps consistent results when re-playing the same part of the game (eg. after a load game)
if (c.HP < weakestHp || (c.HP == weakestHp && c.ID < weakestCharId))
{
weakestEnemy = &gameChars[i];
weakestHp = c.HP;
weakestCharId = c.ID;
}
}
}

return weakestEnemy;
}








share







New contributor




Atazir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$



















    0












    $begingroup$


    I need to improve the efficiency of my program and, using the profiler, have narrowed the problem down to 2 key areas, but I am having trouble coming up with ways to make the program run better.



    Based on my profiler's report, it seems to be telling me that my if functions are inefficient. Whats a better way to achieve a better result?



    Character* FindAttackTarget() const
    {
    float weakestHp = FLT_MAX;
    Character* weakestEnemy = nullptr;
    uint64_t weakestCharId = INT64_MAX;

    //Only attack characters that are within attack range
    auto& gameChars = m_pGame->m_gameCharacters;


    for (size_t i = 0; i < gameChars.size(); ++i)
    {
    auto& c = gameChars[i];
    if (Location.Dist(c.GetLocation()) <= AttackRange &&
    c.HP > 0 &&
    c.Team != Team)
    {
    //We want the weakest with the lowest ID number - this keeps consistent results when re-playing the same part of the game (eg. after a load game)
    if (c.HP < weakestHp || (c.HP == weakestHp && c.ID < weakestCharId))
    {
    weakestEnemy = &gameChars[i];
    weakestHp = c.HP;
    weakestCharId = c.ID;
    }
    }
    }

    return weakestEnemy;
    }








    share







    New contributor




    Atazir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      0












      0








      0





      $begingroup$


      I need to improve the efficiency of my program and, using the profiler, have narrowed the problem down to 2 key areas, but I am having trouble coming up with ways to make the program run better.



      Based on my profiler's report, it seems to be telling me that my if functions are inefficient. Whats a better way to achieve a better result?



      Character* FindAttackTarget() const
      {
      float weakestHp = FLT_MAX;
      Character* weakestEnemy = nullptr;
      uint64_t weakestCharId = INT64_MAX;

      //Only attack characters that are within attack range
      auto& gameChars = m_pGame->m_gameCharacters;


      for (size_t i = 0; i < gameChars.size(); ++i)
      {
      auto& c = gameChars[i];
      if (Location.Dist(c.GetLocation()) <= AttackRange &&
      c.HP > 0 &&
      c.Team != Team)
      {
      //We want the weakest with the lowest ID number - this keeps consistent results when re-playing the same part of the game (eg. after a load game)
      if (c.HP < weakestHp || (c.HP == weakestHp && c.ID < weakestCharId))
      {
      weakestEnemy = &gameChars[i];
      weakestHp = c.HP;
      weakestCharId = c.ID;
      }
      }
      }

      return weakestEnemy;
      }








      share







      New contributor




      Atazir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I need to improve the efficiency of my program and, using the profiler, have narrowed the problem down to 2 key areas, but I am having trouble coming up with ways to make the program run better.



      Based on my profiler's report, it seems to be telling me that my if functions are inefficient. Whats a better way to achieve a better result?



      Character* FindAttackTarget() const
      {
      float weakestHp = FLT_MAX;
      Character* weakestEnemy = nullptr;
      uint64_t weakestCharId = INT64_MAX;

      //Only attack characters that are within attack range
      auto& gameChars = m_pGame->m_gameCharacters;


      for (size_t i = 0; i < gameChars.size(); ++i)
      {
      auto& c = gameChars[i];
      if (Location.Dist(c.GetLocation()) <= AttackRange &&
      c.HP > 0 &&
      c.Team != Team)
      {
      //We want the weakest with the lowest ID number - this keeps consistent results when re-playing the same part of the game (eg. after a load game)
      if (c.HP < weakestHp || (c.HP == weakestHp && c.ID < weakestCharId))
      {
      weakestEnemy = &gameChars[i];
      weakestHp = c.HP;
      weakestCharId = c.ID;
      }
      }
      }

      return weakestEnemy;
      }






      c++ performance





      share







      New contributor




      Atazir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      Atazir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      Atazir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 57 secs ago









      AtazirAtazir

      1




      1




      New contributor




      Atazir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Atazir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Atazir is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Atazir is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217649%2ffinds-a-character-based-on-distance-and-health%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Atazir is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Atazir is a new contributor. Be nice, and check out our Code of Conduct.













          Atazir is a new contributor. Be nice, and check out our Code of Conduct.












          Atazir is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217649%2ffinds-a-character-based-on-distance-and-health%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          is 'sed' thread safeWhat should someone know about using Python scripts in the shell?Nexenta bash script uses...

          How do i solve the “ No module named 'mlxtend' ” issue on Jupyter?

          Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...