Powered by XP-Dev.com - Agile Software Development Tools | You are currently not logged in. Login Now | Don't have a XP-Dev.com account ? Create one for FREE
Open Source Project Trac Project

Zend_Framework Experiences

Experiences on Zend_Framework

Remove these advertisements by upgrading your account for only $5/month

Directory Back to /trunk/Php/array

File /trunk/Php/array/array_rand.php

001: <h1>Test de array_rand()</h1>
002: <pre>
003: <?php
004: require 'Benchmark/Timer.php';
005: $arrItem = str_split('abcdefghi', 1);
006: 
007: function oldRandom ($arrItem)
008: {
009: 
010:     $itemList = array();
011:     if (isset($arrItem)) {
012:         $items_selected_nbr = sizeof($arrItem);
013: 
014:         if ($items_selected_nbr > 2) {
015: 
016:             srand((double) microtime() * 1000000);
017:             $itemList[0] = rand(0, ($items_selected_nbr) - 1);
018: 
019:             do {
020:                 srand((double) microtime() * 1000000);
021:                 $rand = rand(0, ($items_selected_nbr) - 1);
022:             } while (in_array($rand, $itemList));
023:             $itemList[1] = $rand;
024: 
025:             do {
026:                 srand((double) microtime() * 1000000);
027:                 $rand = rand(0, ($items_selected_nbr) - 1);
028:             } while (in_array($rand, $itemList));
029:             $itemList[2] = $rand;
030: 
031: 
032:             $itemList[0] = $arrItem[$itemList[0]];
033:             $itemList[1] = $arrItem[$itemList[1]];
034:             $itemList[2] = $arrItem[$itemList[2]];
035:         }
036:     }
037:     return $itemList;
038: }
039: 
040: 
041: function newRandom ($arrItem, $cnt = 3)
042: {
043: 
044:     return array_intersect_key($arrItem, array_flip(array_rand($arrItem, min($cnt, count($arrItem)))));
045: 
046: }
047: 
048: 
049: function getRandomElement (array $tab, $nb)
050: {
051: 
052:     shuffle($tab);
053:     $result = array_chunk($tab, $nb);
054:     return $result[0];
055: }
056: 
057: 
058: ?>
059: </pre>
060: <h2>Je teste</h2>
061: <pre>
062: <?php
063: 
064: var_export(oldRandom($arrItem));
065: var_export(newRandom($arrItem, 3));
066: var_export(getRandomElement($arrItem, 3));
067: 
068: ?>
069: </pre>
070: <h2>J'en demande plus que disponible</h2>
071: <pre>
072: <?php
073: var_export(oldRandom(array_slice($arrItem, 0, 2)));
074: var_export(newRandom(array_slice($arrItem, 0, 2), 3));
075: var_export(getRandomElement(array_slice($arrItem, 0, 2), 3));
076: 
077: $timer = new Benchmark_Timer();
078: $timer->start();
079: $amount = 500;
080: 
081: $timer->setMarker('oldRandom');
082: $timer->timeElapsed('Start', 'oldRandom') . "\n";
083: for ($i = 0; $i < $amount; $i ++)
084:     oldRandom($arrItem);
085: $timer->timeElapsed('End', 'oldRandom') . "\n";
086: 
087: $timer->setMarker('newRandom');
088: $timer->timeElapsed('Start', 'newRandom') . "\n";
089: for ($i = 0; $i < $amount; $i ++)
090:     newRandom($arrItem, 3);
091: $timer->timeElapsed('End', 'newRandom') . "\n";
092: 
093: $timer->setMarker('getRandomElement');
094: $timer->timeElapsed('Start', 'getRandomElement') . "\n";
095: for ($i = 0; $i < $amount; $i ++)
096:     getRandomElement($arrItem, 3);
097: $timer->timeElapsed('End', 'getRandomElement') . "\n";
098: $timer->timeElapsed('Start', 'oldRandom') . "\n";
099: for ($i = 0; $i < $amount; $i ++)
100:     oldRandom($arrItem);
101: $timer->timeElapsed('End', 'oldRandom') . "\n";
102: 
103: $timer->setMarker('newRandom');
104: $timer->timeElapsed('Start', 'newRandom') . "\n";
105: for ($i = 0; $i < $amount; $i ++)
106:     newRandom($arrItem, 3);
107: $timer->timeElapsed('End', 'newRandom') . "\n";
108: 
109: $timer->setMarker('getRandomElement');
110: $timer->timeElapsed('Start', 'getRandomElement') . "\n";
111: for ($i = 0; $i < $amount; $i ++)
112:     getRandomElement($arrItem, 3);
113: $timer->timeElapsed('End', 'getRandomElement') . "\n";
114: 
115: $timer->stop();
116: $timer->display();
117: 
118: $start = microtime(true);
119: 
120: ?>
121: </pre>
122: <hr />
123: <?php
124: 
125: 
126: highlight_file(__FILE__);
127: ?>

History for /trunk/Php/array/array_rand.php

Diff revisions: vs.

Revision Author Commited Message  
20 Profile+Picture moosh Sat 13 Feb, 2010 14:38:15 +0000

complete with shuffle

Diff Diff
19 Profile+Picture moosh Fri 22 Jan, 2010 06:39:44 +0000

patch some errors

Diff Diff
18 Profile+Picture moosh Fri 22 Jan, 2010 06:17:32 +0000

dump source

Diff Diff
17 Profile+Picture moosh Fri 22 Jan, 2010 06:12:38 +0000

test optimisation for blog post about array_rand()
http://moosh.et.son.brol.be/blog/index.php/2010/02/13/1050-avant-apres-array_rand

History View complete history

Remove these advertisements by upgrading your account for only $5/month

Rss Commits for ZF_experiences:/trunk/Php/array/array_rand.php