tastro Posted September 10, 2011 Share Posted September 10, 2011 i need this code to execute faster, is there a faster way? function all_word_combinations($all_words_unknown,$previous_string=''){ $all_words_2=array(); if(is_array($all_words_unknown)){ $all_words_array=$all_words_unknown; }else{ $all_words_array=explode(' ',trim($all_words_unknown)); } $item2remove='the'; $all_words_array=explode(' ',trim(str_replace($item2remove.' ','',str_replace(' '.$item2remove,'',(implode(' ',$all_words_array)))))); if(count($all_words_array)>1){ $previous_array=explode(' ',$previous_string); foreach($all_words_array as $one_word){ if(!in_array($previous_string.$one_word,$all_words_2) && !in_array($one_word,$previous_array)){ $all_words_2[$previous_string.$one_word]=$previous_string.$one_word; $all_words_2=array_merge($all_words_2,all_word_combinations($all_words_array,$previous_string.$one_word.' ')); } } return $all_words_2; }else{return $all_words_array;} } $one_word_combination=''; foreach(all_word_combinations('cat dog mouse turtle shark whale elephant giraffe lion tiger fish monkey') as $one_word_combination){echo str_replace(' ','-',$one_word_combination).'<br />';} Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/ Share on other sites More sharing options...
redarrow Posted September 10, 2011 Share Posted September 10, 2011 This is a server question the code good inn-it? php runs fast very fast, if it slow it a server problem your host i guess? example i see no power used on your code, i got page's with 4 thousand lines running fast.... Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1267625 Share on other sites More sharing options...
tastro Posted September 10, 2011 Author Share Posted September 10, 2011 This is a server question the code good inn-it? php runs fast very fast, if it slow it a server problem your host i guess? example i see no power used on your code, i got page's with 4 thousand lines running fast.... i need to speed up the code. the server is good enough but the code still runs too slow. also if you can speed up this function somehow, i would appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1267626 Share on other sites More sharing options...
voip03 Posted September 10, 2011 Share Posted September 10, 2011 This is a server question the code good inn-it? php runs fast very fast, if it slow it a server problem your host i guess? example i see no power used on your code, i got page's with 4 thousand lines running fast.... Whatdo u mean by power? Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1267628 Share on other sites More sharing options...
redarrow Posted September 10, 2011 Share Posted September 10, 2011 When i said power, I meant, there no real coding on the page for php to go slow. when i look at php codes,i also look at the way php is set up, most people think having a sheared host, that it, i am done, and all my code will be fast and safe. unfortunately because most sheared hosts use so many accounts on one computer there so slow.(to save money and overheads). Any body that get serious in programming used a dedicated server, there be amazed of the difference and power, from a sheared host. I still say it the host you got, not your code, there nothing you can do unless you go to the server and add a 3rd part application to make php it self go faster, Zend supply such thing.(that your host problem, your currently paying what £5 month what you expect,i pay just under £400 per month so far tilll i grow more big difference as you can see.) Sorry i got no way to make your code go fast ..... maybe a person might come along and have a better idea. have you tried the code with out using the function it self any luck. Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1267631 Share on other sites More sharing options...
voip03 Posted September 10, 2011 Share Posted September 10, 2011 I wouldn’t advise anyone to go for a dedicated server because of the server speed, unless they are receiving 100,000 per day Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1267635 Share on other sites More sharing options...
tastro Posted September 10, 2011 Author Share Posted September 10, 2011 When i said power, I meant, there no real coding on the page for php to go slow. when i look at php codes,i also look at the way php is set up, most people think having a sheared host, that it, i am done, and all my code will be fast and safe. unfortunately because most sheared hosts use so many accounts on one computer there so slow.(to save money and overheads). Any body that get serious in programming used a dedicated server, there be amazed of the difference and power, from a sheared host. I still say it the host you got, not your code, there nothing you can do unless you go to the server and add a 3rd part application to make php it self go faster, Zend supply such thing.(that your host problem, your currently paying what £5 month what you expect,i pay just under £400 per month so far tilll i grow more big difference as you can see.) Sorry i got no way to make your code go fast ..... maybe a person might come along and have a better idea. have you tried the code with out using the function it self any luck. i have a dedicated server. just want to speed up the code. Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1267680 Share on other sites More sharing options...
redarrow Posted September 10, 2011 Share Posted September 10, 2011 Slap this on the page we want the result posted for statistics. <?php function get_execution_time() { static $microtime_start = null; if($microtime_start === null) { $microtime_start = microtime(true); return 0.0; } return microtime(true) - $microtime_start; } get_execution_time(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1267686 Share on other sites More sharing options...
tastro Posted September 10, 2011 Author Share Posted September 10, 2011 this one is better i think... we want to see the average of course: <?php function all_word_combinations($all_words_unknown,$previous_string=''){ $all_words_2=array(); if(is_array($all_words_unknown)){ $all_words_array=$all_words_unknown; }else{ $all_words_array=explode(' ',trim($all_words_unknown)); } $item2remove='the'; $all_words_array=explode(' ',trim(str_replace($item2remove.' ','',str_replace(' '.$item2remove,'',(implode(' ',$all_words_array)))))); if(count($all_words_array)>1){ $previous_array=explode(' ',$previous_string); foreach($all_words_array as $one_word){ if(!in_array($previous_string.$one_word,$all_words_2) && !in_array($one_word,$previous_array)){ $all_words_2[$previous_string.$one_word]=$previous_string.$one_word; $all_words_2=array_merge($all_words_2,all_word_combinations($all_words_array,$previous_string.$one_word.' ')); } } return $all_words_2; }else{return $all_words_array;} } ////////////////////////// ////////////////////////// ////////////////////////// // TEST START ////////////////////////// ////////////////////////// ////////////////////////// $runs=100; $microtime_start=microtime(true); for($i=0;$i<$runs;$i++){ ////////////////////////// ////////////////////////// ////////////////////////// // CODE TO TEST ////////////////////////// ////////////////////////// ////////////////////////// $one_word_combination=''; foreach(all_word_combinations('cat dog mouse turtle shark whale elephant giraffe lion tiger fish monkey') as $one_word_combination){echo str_replace(' ','-',$one_word_combination).'<br />';} ////////////////////////// ////////////////////////// ////////////////////////// // CODE TO TEST ////////////////////////// ////////////////////////// ////////////////////////// } $microtime_end=microtime(true); $time_of_all_runs=$microtime_end-$microtime_start; $average_time_for_one_run=(($microtime_end-$microtime_start)/$runs); echo 'Time for all runs: <b>'.number_format($time_of_all_runs,4).'</b> <br /> Average time for one run <b>'.number_format($average_time_for_one_run,7).'</b>'; ?> it takes too long... longer then the max. execution time. it takes too long already without the foreach() at the end. Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1267735 Share on other sites More sharing options...
Pikachu2000 Posted September 10, 2011 Share Posted September 10, 2011 Have you actually figured out how many results that will produce? Just looking at it, I can tell it won't be a trivial number. Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1267747 Share on other sites More sharing options...
xyph Posted September 10, 2011 Share Posted September 10, 2011 Please also tell us what you're trying to accomplish. From what I gather, you're inputting a string of space separated words and you want all combinations of the words. Did you want two word strings as well as x word strings? Does order matter? Example: string 'tiger shark bear' would return 'tiger' 'tiger shark' 'tiger bear' 'tiger shark bear' 'tiger bear shark' 'bear' 'bear tiger' 'bear shark' 'bear tiger shark' 'bear shark tiger' 'shark' 'shark tiger' 'shark bear' 'shark tiger bear' 'shark bear tiger' ? Please elaborate. Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1267757 Share on other sites More sharing options...
tastro Posted September 10, 2011 Author Share Posted September 10, 2011 Please also tell us what you're trying to accomplish. From what I gather, you're inputting a string of space separated words and you want all combinations of the words. Did you want two word strings as well as x word strings? Does order matter? Example: string 'tiger shark bear' would return 'tiger' 'tiger shark' 'tiger bear' 'tiger shark bear' 'tiger bear shark' 'bear' 'bear tiger' 'bear shark' 'bear tiger shark' 'bear shark tiger' 'shark' 'shark tiger' 'shark bear' 'shark tiger bear' 'shark bear tiger' ? Please elaborate. i just need an array of all the word combinations. order doesn't matter. all combinations. here is an example: cat cat dog cat dog mouse cat mouse cat mouse dog dog dog cat dog cat mouse dog mouse dog mouse cat mouse mouse cat mouse cat dog mouse dog mouse dog cat Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1267801 Share on other sites More sharing options...
tastro Posted September 10, 2011 Author Share Posted September 10, 2011 Have you actually figured out how many results that will produce? Just looking at it, I can tell it won't be a trivial number. yes i know, there will be many combinations. Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1267805 Share on other sites More sharing options...
xyph Posted September 11, 2011 Share Posted September 11, 2011 i just need an array of all the word combinations. order doesn't matter. all combinations. here is an example: cat cat dog cat dog mouse cat mouse cat mouse dog dog dog cat dog cat mouse dog mouse dog mouse cat mouse mouse cat mouse cat dog mouse dog mouse dog cat If order doesn't matter, then mouse dog cat = cat dog mouse Please clarify, as what you stated contradicts your example. Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1267921 Share on other sites More sharing options...
tastro Posted September 11, 2011 Author Share Posted September 11, 2011 i just need an array of all the word combinations. order doesn't matter. all combinations. here is an example: cat cat dog cat dog mouse cat mouse cat mouse dog dog dog cat dog cat mouse dog mouse dog mouse cat mouse mouse cat mouse cat dog mouse dog mouse dog cat If order doesn't matter, then mouse dog cat = cat dog mouse Please clarify, as what you stated contradicts your example. oh... you meant that order... then yes... here it matters. i need both. cat dog mouse and mouse dog cat Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1267948 Share on other sites More sharing options...
xyph Posted September 11, 2011 Share Posted September 11, 2011 But do you also need cat mouse dog cat dog mouse ? Order mattering sometimes could make this a complicated script. Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1268041 Share on other sites More sharing options...
tastro Posted September 11, 2011 Author Share Posted September 11, 2011 now i figured that it could work and it would be faster if i would have only cat-dog-mouse instead of all the combinations. but cat-dog-mouse must be sorted from a-z. also i need cat-dog-mouse cat-dog cat-mouse dog-mouse cat dog mouse but also it must work for more words too. not only for 3. Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1268062 Share on other sites More sharing options...
xyph Posted September 11, 2011 Share Posted September 11, 2011 <?php $array = array( 'cat','mouse','dog','horse' ); $result = getCombinations($array); sort( $result ); print_r( $result ); function getCombinations($a) { $r = array(); $l = count($a); $t = pow(2,$l); for ( $i = 1; $i < $t; $i++ ) { $b=str_pad(decbin($i), $l, '0', STR_PAD_LEFT); $c=''; for( $j = 0; $j < $l; $j++ ) { if( $b[$j] == 1 ) $c .= $a[$j]; } $r[] = $c ; } return $r; } ?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1268089 Share on other sites More sharing options...
Pandemikk Posted September 11, 2011 Share Posted September 11, 2011 http://www.lampdocs.com/blog/2008/08/how-to-create-all-possible-word-combinations-from-a-string-with-php/ Sometimes Google helps. Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1268101 Share on other sites More sharing options...
xyph Posted September 11, 2011 Share Posted September 11, 2011 http://www.lampdocs.com/blog/2008/08/how-to-create-all-possible-word-combinations-from-a-string-with-php/ Sometimes Google helps. Sometimes reading the thread helps. Not only is that not what the poster wants, that's permutations, not combinations. When an article can't get the English correct, can you trust the code? Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1268148 Share on other sites More sharing options...
Pandemikk Posted September 11, 2011 Share Posted September 11, 2011 http://www.lampdocs.com/blog/2008/08/how-to-create-all-possible-word-combinations-from-a-string-with-php/ Sometimes Google helps. Sometimes reading the thread helps. Not only is that not what the poster wants, that's permutations, not combinations. When an article can't get the English correct, can you trust the code? Surely there's no need for such hostility, is there xyph? Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1268155 Share on other sites More sharing options...
tastro Posted September 12, 2011 Author Share Posted September 12, 2011 <?php $array = array( 'cat','mouse','dog','horse' ); $result = getCombinations($array); sort( $result ); print_r( $result ); function getCombinations($a) { $r = array(); $l = count($a); $t = pow(2,$l); for ( $i = 1; $i < $t; $i++ ) { $b=str_pad(decbin($i), $l, '0', STR_PAD_LEFT); $c=''; for( $j = 0; $j < $l; $j++ ) { if( $b[$j] == 1 ) $c .= $a[$j]; } $r[] = $c ; } return $r; } ?> thank you very much xyph! your code works much faster then mine! one more thing... i would really appreciate it, if you could explain to me what pow() function does. i checked on php.net already but i don't get it. :S pow(2,9) returns 512... and i can't figure out what it does with numbers 2 and 9 to get out 512. and how would the code look like without this two functions? str_pad() and decbin() is it possible to use some other ones instead of this 2? also if someone of you has even and faster way of executing this, then post it please. thank you! Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1268175 Share on other sites More sharing options...
Pandemikk Posted September 12, 2011 Share Posted September 12, 2011 http://php.net/manual/en/function.pow.php Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1268176 Share on other sites More sharing options...
tastro Posted September 12, 2011 Author Share Posted September 12, 2011 http://php.net/manual/en/function.pow.php i said in my previous reply already that i can't figure it out on php.net i already red all the info there, not once. also if you could explain it to me in some other "easier" way it would be nice. Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1268178 Share on other sites More sharing options...
tastro Posted September 12, 2011 Author Share Posted September 12, 2011 http://www.lampdocs.com/blog/2008/08/how-to-create-all-possible-word-combinations-from-a-string-with-php/ Sometimes Google helps. this one is even slower then mine. but thanks for trying anyways. Quote Link to comment https://forums.phpfreaks.com/topic/246835-i-need-this-code-to-execute-faster/#findComment-1268180 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.