Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Generate Array of threads 7000
- */
- function makeThreads()
- {
- for($i=1000; $i<=8000; $i++)
- {
- yield array(
- $i*10+1,
- $i*10+2,
- $i*10+3,
- $i*10+4,
- $i*10+5,
- );
- }
- };
- $threads = iterator_to_array(makeThreads());
- /**
- * Generate list of 35000 Emails
- */
- function makeEmails()
- {
- for($i=1000; $i<=8000; $i++)
- {
- for($e=1; $e<=5; $e++)
- {
- $email = new stdClass;
- $email->subject = 'Cloud Storage Dump';
- $email->to = 'jondoe@domain.com';
- $email->msgno = $i*10+$e;
- yield $email;
- }
- }
- };
- $emails = iterator_to_array(makeEmails());
- $then = microtime();
- $thenM = memory_get_peak_usage();
- /**
- * TEST: 3 x foreach()
- *
- * Takes: 2,8 s
- * Memory: 5,2 MB
- */
- /*
- // root is the array index position of the threads message, such as 5 or 10
- foreach($threads as $root => $message){
- // id is the id being given to us from `imap_thread`
- foreach($message as $key => $id){
- foreach($emails as $index => $email){
- if($id === $email->msgno){
- $threads[$root][$key] = $email;
- break;
- }
- }
- }
- }
- */
- /**
- * TEST: PHP 5.3/5.4 way
- *
- * Takes: 0.061 s
- * Memory: 2.7 MB
- */
- /*
- // Create map of key-threads
- $keys = array();
- foreach($emails as $k => $email)
- {
- $keys[$email->msgno] = $k;
- }
- // Iterate threads
- $threads = array_map(function($thread) use($emails, $keys)
- {
- // Iterate emails in these threads
- return array_map(function($msgno) use($emails, $keys)
- {
- // Swap the msgno with the email details
- return $emails[$keys[$msgno]];
- }, $thread);
- }, $threads);
- */
- /**
- * TEST: PHP 5.5 way
- *
- * Takes: 0.036 s
- * Memory: 2.7 MB
- */
- // Create map of key-threads
- function genetateKeyMap($emails)
- {
- foreach($emails as $k => $email)
- {
- yield $email->msgno => $k;
- }
- };
- $keys = iterator_to_array(genetateKeyMap($emails));
- // Assign email details to thread map
- function updateThreads($emails, $threads, $keys)
- {
- foreach($threads as $thread)
- {
- $array = array();
- foreach($thread as $msgno)
- {
- $array[] = $emails[$keys[$msgno]];
- }
- yield $array;
- }
- };
- $threads = iterator_to_array(updateThreads($emails, $threads, $keys));
- /**
- * Profile
- */
- $memoryTaken = memory_get_peak_usage() - $thenM;
- echo 'Took: '. (microtime() - $then) .' miliseconds';
- echo '<br>';
- echo 'Memory used: '. number_format($memoryTaken/1024/1024, 1) .' MB';
- echo '<br>';
- echo '<br><hr><br /><pre>'. print_r($threads[0], true). '</pre>';
- echo '<br><hr><br /><pre>'. print_r($threads[8000], true). '</pre>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement