title: Java method running very slowly on first run only (Android)
body: <p>I have found that my method called checkForErrors takes around 80x longer to run on the first execution only, and I can't seem to figure out why.</p>
  
  <pre><code>D/guess: adhl
  D/checkerror: checking for errors took 4760743ns // first run
  D/validity: checking guess validity took 7141114ns
  D/guess: agkl
  D/checkerror: checking for errors took 61035ns // every other run takes around this long
  D/validity: checking guess validity took 732422ns
  </code></pre>
  
  <p>I have looked through the code and I don't see anything that would take longer on the first run only so I'm stumped.</p>
  
  <p>Button on click listener:</p>
  
  <pre><code>submitBtn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              String Guess = guess_txt.getText().toString();
              Log.d("guess", Guess);
              if(checkGuessValidity(Guess)){ // &lt;--
                  submitValidGuess(Guess);
                  int bulls = game.getBullsAndHits()[0];
                  int hits = game.getBullsAndHits()[1];
                  if(!game.gameWon) //if game is not won, submit the guess with bulls and hits
                      guessSubmittedListener.guessSubmitted(Guess, bulls, hits);
                  else //if game is won, call gameWon() method
                      gameEnd();
              }
              if((game.getCurrentTry() &gt; game.getMaxTries()) &amp;&amp; (!Guess.equals(game.getHiddenWord()))) gameEnd(); //if user is out of tries, call gameLost method
  
              triesLeft.setText("Tries Left: " + (game.getMaxTries() - game.getCurrentTry() + 1)); //update tries left on main fragment
              guess_txt.setText("");
          }
      });
  </code></pre>
  
  <p>checkGuessValidity:</p>
  
  <pre><code>public boolean checkGuessValidity(String Guess){
      long start = System.nanoTime();
      long start2 = System.nanoTime();
      ErrorList Status = checkForErrors(Guess); // &lt;--
      long end2 = System.nanoTime();
  
      Log.d("checkerror", "checking for errors took " + (end2 - start2) + "ns");
      . . . more code . . .
  </code></pre>
  
  <p>checkForErrors:</p>
  
  <pre><code>public ErrorList checkForErrors(String Guess){
      if(Guess.length() != game.getHiddenWordLength()) return ErrorList.Wrong_Length;
      else if(!isValidInput(Guess)) return ErrorList.Invalid_Characters;
      else if(!isIsogram(Guess)) return ErrorList.Not_Isogram;
      else if(!isLowercase(Guess)) return ErrorList.Not_Lowercase;
      else return ErrorList.OK;
  }
  </code></pre>
  
  <p>isValidInput, isIsogram and isLowercase:</p>
  
  <pre><code>public boolean isIsogram(String Guess){
      Map&lt;Character, Boolean&gt; map = new HashMap();
  
      for(int i = 0; i &lt; Guess.length(); i++){
          if(map.get(Guess.charAt(i)) == null) //if the value for the key (character) is null (has not been changed since map initialization)
              map.put(Guess.charAt(i), true); //then set it to true (indicating that it has been seen)
          else { //else (if the value at the character HAS been changed since initialization, ie. it has been seen)
              Log.d("Character repeated", "" + Guess.charAt(i));
              return false; //return false
          }
      }
      return true; //if loop completes no duplicates were found and guess is an isogram
  }
  
  public boolean isLowercase(String Guess){
      if(Guess.equals(Guess.toLowerCase())) return true;
      else return false;
  }
  
  public boolean isValidInput(String Guess){
      char[] chars = Guess.toCharArray();
      for(int i = 0; i &lt; chars.length; i++){
          if(!isLatinLetter(chars[i])) return false;
      }
      return true;
  }
  
  public static boolean isLatinLetter(char c) {
      return (c &gt;= 'A' &amp;&amp; c &lt;= 'Z') || (c &gt;= 'a' &amp;&amp; c &lt;= 'z');
  }
  </code></pre>
  
  <p>It doesn't seem like any of the methods should be impacted by when they are run, so I don't know why it takes extra long on the first execution. I'm still a beginner to programming so please excuse any poor formatting or horribly optimized code :p .</p>
  
  <p>edit: CPU usage graph: <a href="https://prnt.sc/ftjokq" rel="nofollow noreferrer">https://prnt.sc/ftjokq</a></p>