Answers.Body: <p>Something like this should work and be faster.</p>
  
  <pre class="lang-php prettyprint-override"><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
  $criteria-&gt;section = 'journalArticles';
  $criteria-&gt;limit = 10;
  $entries = $criteria-&gt;find();
  
  $titles = array();
  $copies = array();
  
  foreach ($entries as $entry)
  {
      $titles[] = $entry-&gt;title;
  }
  
  foreach (array_count_values($titles) as $val =&gt; $count)
  {
      if ($count &gt; 1)
      {
          $copies[] = $val;
      }
  }
  
  $this-&gt;returnJson($copies);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-27T18:51:44.637Z
Answers.Text: This is legit. I have it working now. Thanks a lot for your help.
Answers.UserDisplayName: Dan Zuzevich
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-27T17:49:52.080Z
Answers.LastActivityDate: 2017-12-28T19:12:22.867Z
Answers.LastEditDate: 2017-12-28T19:12:22.867Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <p>Just some side notes as an explanation why your code takes so long and to understand Craft a little bit more
  your <code>count()</code> function does actually this:</p>
  
  
  
  <pre class="lang-php prettyprint-override"><code>public function count()
  {
      // If the query has already been executed, just return a count of the results.
      if (isset($this-&gt;_matchedElements))
      {
          return count($this-&gt;_matchedElements);
      }
  
      $total = $this-&gt;total();
  
      if ($this-&gt;offset)
      {
          $total -= $this-&gt;offset;
  
          if ($total &lt; 0)
          {
              $total = 0;
          }
      }
  
      if ($this-&gt;limit &amp;&amp; $total &gt; $this-&gt;limit)
      {
          $total = $this-&gt;limit;
      }
  
      return $total;
  }
  </code></pre>
  
  <p>So it checks if there are cached elements and if not the <code>total</code> function is executed</p>
  
  <pre class="lang-php prettyprint-override"><code>public function total($attributes = null)
  {
      $this-&gt;setAttributes($attributes);
  
      $this-&gt;_includeInTemplateCaches();
  
      if (!isset($this-&gt;_cachedTotal))
      {
          $this-&gt;_cachedTotal = craft()-&gt;elements-&gt;getTotalElements($this);
      }
  
      return $this-&gt;_cachedTotal;
  }
  </code></pre>
  
  <p>Which only caches the number but <strong>not</strong> the elements</p>
  
  <p>So what you actually do is executing the query about 100 times (because of your two loops) </p>
  
  <p>Just using <code>$entries = $criteria-&gt;find()</code> and looping through your entries is a far better idea</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-27T18:11:44.263Z
Answers.Text: I should have just waited on you to answer. :)
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-27T18:24:24.540Z
Answers.Text: It's good you answered first because I would have done some nasty workaround since I forgot about the `array_count_values()` function. With your answer he has a solid code, I just wanted to show some things behind the scene (I hope this is the correct reason why it took so long because the longer I look at the code the more I have the feeling I make a fool of myself)
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-27T18:38:28.833Z
Answers.Text: Testing the code out now ~_~
Answers.UserDisplayName: Dan Zuzevich
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-27T18:54:41.370Z
Answers.Text: Thanks again for diving deep and explaining whats going on behind the scenes. So what your saying is that since I did not use the find method, and set that result to a variable, I am querying the database for the elements 100 times? Holy crapola.
Answers.UserDisplayName: Dan Zuzevich
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-27T19:19:02.260Z
Answers.Text: @RobinSchambach Nope, I think you're right... the first example was basically O(n²) algorithm, so with 10 inputs you get 100 executions.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-27T18:09:55.063Z
Answers.LastActivityDate: 2017-12-28T16:18:02.140Z
Answers.LastEditDate: 2017-12-28T16:18:02.140Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I am attempting to loop through my "journalEntries" in order to find duplicates. For some reason, it is taking a very long time to finish and return a response to me. Last time I tested it, it took 12 seconds to finish.</p>
  
  <pre class="lang-php prettyprint-override"><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
  $criteria-&gt;section = 'journalArticles';
  $criteria-&gt;limit = 10;
  $copies = array();
  
  for($a = 0; $a &lt; count($criteria); $a++) {
    for($b = $a + 1; $b &lt; count($criteria); $b++) {
      if($criteria[$a]-&gt;title === $criteria[$b]-&gt;title) {
        array_push($copies, $criteria[$a]-&gt;title);
      }
    }
  }
  
  $this-&gt;returnJson($copies);
  </code></pre>
  
  <p>I think it is something I am doing incorrectly within the two for loops. As you can see, I limit the amount of articles to only 10, so something seriously funky must be going on. Any help?</p>
  
Tags: <entries><php>
Title: Loop through entries taking far too long to finish
ThreadText: # Loop through entries taking far too long to finish
  
  (Asked by: username_0 on 2017-12-27)
  
  I am attempting to loop through my "journalEntries" in order to find duplicates. For some reason, it is taking a very long time to finish and return a response to me. Last time I tested it, it took 12 seconds to finish.
  
  ```php
  $criteria = craft()->elements->getCriteria(ElementType::Entry);
  $criteria->section = 'journalArticles';
  $criteria->limit = 10;
  $copies = array();
  
  for($a = 0; $a < count($criteria); $a++) {
    for($b = $a + 1; $b < count($criteria); $b++) {
      if($criteria[$a]->title === $criteria[$b]->title) {
        array_push($copies, $criteria[$a]->title);
      }
    }
  }
  
  $this->returnJson($copies);
  
  ```
  
  I think it is something I am doing incorrectly within the two for loops. As you can see, I limit the amount of articles to only 10, so something seriously funky must be going on. Any help?
  
  **Tags:** entries, php
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-12-27. Score: 3 | Accepted answer
  
  Something like this should work and be faster.
  
  ```php
  $criteria = craft()->elements->getCriteria(ElementType::Entry);
  $criteria->section = 'journalArticles';
  $criteria->limit = 10;
  $entries = $criteria->find();
  
  $titles = array();
  $copies = array();
  
  foreach ($entries as $entry)
  {
      $titles[] = $entry->title;
  }
  
  foreach (array_count_values($titles) as $val => $count)
  {
      if ($count > 1)
      {
          $copies[] = $val;
      }
  }
  
  $this->returnJson($copies);
  
  ```
  
  > **Comments:**
  > **username_0**: This is legit. I have it working now. Thanks a lot for your help.  
  
  ---
  
  ### Answer by username_2 on 2017-12-27. Score: 3
  
  Just some side notes as an explanation why your code takes so long and to understand Craft a little bit more
  your `count()` function does actually this:
  
  ```php
  public function count()
  {
      // If the query has already been executed, just return a count of the results.
      if (isset($this->_matchedElements))
      {
          return count($this->_matchedElements);
      }
  
      $total = $this->total();
  
      if ($this->offset)
      {
          $total -= $this->offset;
  
          if ($total < 0)
          {
              $total = 0;
          }
      }
  
      if ($this->limit && $total > $this->limit)
      {
          $total = $this->limit;
      }
  
      return $total;
  }
  
  ```
  
  So it checks if there are cached elements and if not the `total` function is executed
  
  ```php
  public function total($attributes = null)
  {
      $this->setAttributes($attributes);
  
      $this->_includeInTemplateCaches();
  
      if (!isset($this->_cachedTotal))
      {
          $this->_cachedTotal = craft()->elements->getTotalElements($this);
      }
  
      return $this->_cachedTotal;
  }
  
  ```
  
  Which only caches the number but **not** the elements
  
  So what you actually do is executing the query about 100 times (because of your two loops)
  
  Just using `$entries = $criteria->find()` and looping through your entries is a far better idea
  
  > **Comments:**
  > **username_1**: I should have just waited on you to answer. :)  
  > **username_2**: It's good you answered first because I would have done some nasty workaround since I forgot about the `array_count_values()` function. With your answer he has a solid code, I just wanted to show some things behind the scene (I hope this is the correct reason why it took so long because the longer I look at the code the more I have the feeling I make a fool of myself)  
  > **username_0**: Testing the code out now ~_~  
  > **username_0**: Thanks again for diving deep and explaining whats going on behind the scenes. So what your saying is that since I did not use the find method, and set that result to a variable, I am querying the database for the elements 100 times? Holy crapola.  
  > **username_1**: @username_2 Nope, I think you're right... the first example was basically O(n²) algorithm, so with 10 inputs you get 100 executions.  
  
  ---
  
  