title: Single statement consuming ~1000 ms to execute - Is it obvious?
body: <p>rerouted from <a href="https://drupal.stackexchange.com/questions/83846/single-statement-consuming-1000-ms-to-execute-is-it-obvious">here</a>  </p>
  
  <p>The <code>concat.=$concat .= $ref_wrapper-&gt;$er_field_mac_id-&gt;nid-&gt;value();</code> line is consuming more time to execute. Is it obvious or is there some thing else that might cause this behaviour.</p>
  
  <p>What am I doing?<br>
  1.Iterate field collection item as it is a multi value field<br>
  2.Iterate though field inside field_collection item to get field whose machine name ends with <strong>_list_er</strong> (this is a entity reference field)<br>
  3.Once machine name found break and do not enter second loop again<br>
  4 Continue with outer loop and set the $concat variable  </p>
  
  <p>Why loop to get machine name?<br>
  I do not want it hardcoded like field_item_list_er, because same snippet should be applied to different fields that ends tith _list_er. Ex. field_tv_list_er, field_favteam_list_er. </p>
  
  <pre><code>timer_start('get_loop_time');
  foreach ($wrapper-&gt;$list_mac_id-&gt;getIterator() as $delta =&gt; $ref_wrapper) {
      if (!isset($er_field_mac_id)) {
          foreach ($ref_wrapper-&gt;getIterator() as $in_fci) {
              $in_fci = $in_fci-&gt;info();
              if (is_array($in_fci)) {
                  if (is_array($in_fci) &amp;&amp; isset($in_fci['type']) &amp;&amp; $in_fci['type'] == 'node' &amp;&amp; preg_match(
                          "/_list_er\z/i",
                          $in_fci['name']
                      )
                  ) {
                      $er_field_mac_id = $in_fci['name'];
                      break 1;
                  }
              }
          }
      }
      //following statement consuming from ~600ms to ~750ms for each iteration(currently only 2)
      timer_start('get_single_time');
      $concat .= $ref_wrapper-&gt;$er_field_mac_id-&gt;nid-&gt;value();
      dpm(timer_read('get_single_time'));
  }
  //so then total is from ~1200ms to ~1400 ms  (includes for loop)
  dpm(timer_read('get_loop_time'));
  </code></pre>
  
  <p><strong>update: apologies :( for lack of info,now details are updated.One more thing there are > 10000 node are present to which entityreference field is referenced.</strong> </p>
  
  <p><strong>update2:while seaching for the solution I found the <a href="http://audiblecode.com/blog/programmatically-create-and-update-field-collection-item-node-entity-api" rel="nofollow noreferrer">link</a> about accessing field_collection_item and further splitted the statement like below</strong></p>
  
  <pre><code>// Load that field collection item
  $field_collection_item_value = $ref_wrapper-&gt;value();
  
  // Wrap it with Entity API
  $collection = entity_metadata_wrapper('field_collection_item', $ref_wrapper);
  
  $val = $collection-&gt;{$er_field_mac_id}-&gt;value()-&gt;nid;
  </code></pre>
  
  <p>From above code it is found that statement <code>$field_collection_item_value = $ref_wrapper-&gt;value();</code> is consuming more time around 600ms for each iteration, where $ref_wrapper is a field collection item. Is it a problem with the field collection item for taking so much  of time for loading single field value(inside field_collection_item two fields are there one is entityreference and another is textfield) </p>