Jump to content

i need this code to execute faster


tastro

Recommended Posts

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 />';}

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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.

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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();
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites


<?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]

Link to comment
Share on other sites

 

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?

Link to comment
Share on other sites

 

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?  8)

 

 

Link to comment
Share on other sites

<?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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.