Advertisement
Guest User

Map of email threads - Test

a guest
Jul 9th, 2013
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.62 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. /**
  5.  * Generate Array of threads 7000
  6.  */
  7. function makeThreads()
  8. {
  9.     for($i=1000; $i<=8000; $i++)
  10.     {
  11.         yield array(
  12.             $i*10+1,
  13.             $i*10+2,
  14.             $i*10+3,
  15.             $i*10+4,
  16.             $i*10+5,
  17.         );
  18.     }
  19. };
  20. $threads = iterator_to_array(makeThreads());
  21.  
  22. /**
  23.  * Generate list of 35000 Emails
  24.  */
  25. function makeEmails()
  26. {
  27.     for($i=1000; $i<=8000; $i++)
  28.     {
  29.         for($e=1; $e<=5; $e++)
  30.         {
  31.             $email = new stdClass;
  32.             $email->subject = 'Cloud Storage Dump';
  33.             $email->to = 'jondoe@domain.com';
  34.             $email->msgno = $i*10+$e;
  35.             yield $email;
  36.         }
  37.     }
  38. };
  39. $emails = iterator_to_array(makeEmails());
  40.  
  41. $then  = microtime();
  42. $thenM = memory_get_peak_usage();
  43.  
  44.  
  45.  
  46. /**
  47.  * TEST: 3 x foreach()
  48.  *
  49.  * Takes: 2,8 s
  50.  * Memory: 5,2 MB
  51.  */
  52.  
  53. /*
  54. // root is the array index position of the threads message, such as 5 or 10
  55. foreach($threads as $root => $message){
  56.  
  57.     // id is the id being given to us from `imap_thread`
  58.     foreach($message as $key => $id){
  59.  
  60.       foreach($emails as $index => $email){
  61.  
  62.          if($id === $email->msgno){
  63.              $threads[$root][$key] = $email;
  64.              break;
  65.           }
  66.       }
  67.     }
  68. }
  69. */
  70.  
  71.  
  72. /**
  73.  * TEST: PHP 5.3/5.4 way
  74.  *
  75.  * Takes: 0.061 s
  76.  * Memory: 2.7 MB
  77.  */
  78.  
  79. /*
  80. // Create map of key-threads
  81. $keys = array();
  82. foreach($emails as $k => $email)
  83. {
  84.     $keys[$email->msgno] = $k;
  85. }
  86.  
  87. // Iterate threads
  88. $threads = array_map(function($thread) use($emails, $keys)
  89. {
  90.     // Iterate emails in these threads
  91.     return array_map(function($msgno) use($emails, $keys)
  92.     {
  93.         // Swap the msgno with the email details
  94.         return $emails[$keys[$msgno]];
  95.  
  96.     }, $thread);
  97.  
  98. }, $threads);
  99. */
  100.  
  101.  
  102. /**
  103.  * TEST: PHP 5.5 way
  104.  *
  105.  * Takes: 0.036 s
  106.  * Memory: 2.7 MB
  107.  */
  108.  
  109.  
  110. // Create map of key-threads
  111. function genetateKeyMap($emails)
  112. {
  113.     foreach($emails as $k => $email)
  114.     {
  115.         yield $email->msgno => $k;
  116.     }
  117. };
  118. $keys = iterator_to_array(genetateKeyMap($emails));
  119.  
  120.  
  121. // Assign email details to thread map
  122. function updateThreads($emails, $threads, $keys)
  123. {
  124.     foreach($threads as $thread)
  125.     {
  126.         $array = array();
  127.         foreach($thread as $msgno)
  128.         {
  129.             $array[] = $emails[$keys[$msgno]];
  130.         }
  131.         yield $array;
  132.     }
  133. };
  134. $threads = iterator_to_array(updateThreads($emails, $threads, $keys));
  135.  
  136.  
  137.  
  138.  
  139. /**
  140.  * Profile
  141.  */
  142.  
  143. $memoryTaken = memory_get_peak_usage() - $thenM;
  144.  
  145. echo 'Took: '. (microtime() - $then) .' miliseconds';
  146. echo '<br>';
  147. echo 'Memory used: '. number_format($memoryTaken/1024/1024, 1) .' MB';
  148.  
  149. echo '<br>';
  150. echo '<br><hr><br /><pre>'. print_r($threads[0], true). '</pre>';
  151. echo '<br><hr><br /><pre>'. print_r($threads[8000], true). '</pre>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement