Thank you for being a valued part of the CNET community. As of December 1, 2020, the forums are in read-only format. In early 2021, CNET Forums will no longer be available. We are grateful for the participation and advice you have provided to one another over the years.

Thanks,

CNET Support

Resolved Question

cpu speed and php script execution time

Apr 8, 2017 10:40AM PDT

Moving PHP script from 2.7 to 2.8 ghz dual core i7 mac book pro, reduced execution time of php script by 1.5 hrs down from 20 hr total time. Would an alienware 4.4 ghz dual core i7 decrease execution time of php script at least 14 hrs? Or do the speed ratios not work that way?

Discussion is locked

CharlesDriver has chosen the best answer to their question. View answer

Best Answer

- Collapse -
Could.
Apr 8, 2017 10:48AM PDT

I don't know what this code is doing so I can't see if it's just linear or is there disk I/O that needs help.

OR the bigger question of algorithms. You can get more out of an algorithm than brute force.

If you can, ask for a code review at say daniweb.com. If you phrase it right and not sound demanding they might look it over if it's not hundreds of lines long.

https://www.google.com/search?q=optimizing+php gives us a head start.

- Collapse -
php script speed up with cpu
Apr 8, 2017 11:24AM PDT

Thank you for your reply,

The php script is taking quite sometime because it is using millions of iterations of merging very large numerical strings and removing duplicates and then pulling all the strings that fell out 14 out of 15 iterations.

I can't see any other way I can optimize that process any more than I already have. Without doing anything, my script improved 1.5 hrs with a cpu change, so I was hoping to throw cpu at it to speed it up.

- Collapse -
Or....
Apr 8, 2017 11:31AM PDT

You look at the CPU load during the run. It's not a sure thing that all CPU cores are being put to use in some apps. So what to do? In a system I wrote long ago I would "spin up" worker threads and each would and did run on its own core. Apple OS, Linux, Windows and other OSes will put these on the less laden core and my app dropped in linear measurements as more CPU cores were available.

Moving from 2 to 4 cores in my app using this method almost dropped the run time in half.

That's partially why I think algorithms and matching the algorithm to your hardware and OS can really pay off. Most computer scientists were taught in classic methods that didn't account for multiple CPUs (or cores.)

- Collapse -
CPU load run
Apr 8, 2017 12:35PM PDT

I do appreciate your help and patience!

I am not familiar with "cpu load during run"...do you know of a link to youtube or an article that would show me how to check this on a mac os? And also, generally how would I interpret the results, high cpu load score vs. low cpu score?

- Collapse -
cpu load run
Apr 8, 2017 12:39PM PDT

It appears that the script is not cpu intensive whatsoever...is there away to check if it is using both cores?

- Collapse -
That's the activity monitor found in itsdigger's link.
Apr 8, 2017 12:51PM PDT

If the CPUs are not being tapped, could this be I/O bound?

Remember I don't know your app but you do.

- Collapse -
cpu load run
Apr 8, 2017 1:09PM PDT

So, I may have been wrong about the low cpu part...If I am reading activity monitor correctly, when I run the script, the php process load is at 94% and the terminal load is at 200% because I run the script at CLI. Does this mean anything of significance?

- Collapse -
Not yet.
Apr 8, 2017 1:22PM PDT

I don't know your code and it appears that it's tasking both CPU cores so in car talk, firing on all cylinders.

Did you try a PHP compiler?
https://www.thefreecountry.com/compilers/php.shtml

Remember that algorithms pay off more than faster machines. But do try compiling it.

- Collapse -
Php compiler and pthreads
Apr 8, 2017 3:36PM PDT

Thanks for this hint, I am looking into pthread extension as well and will let you know how that goes. Will try the php compilers and update results.

- Collapse -
Answer
Still haven't seen the code.
Apr 8, 2017 2:18PM PDT
http://phpfiddle.org/ may give you a way to run this online without taxing your computer.

I did watch for you to ask at daniweb but didn't see it.
- Collapse -
Code is attached
Apr 8, 2017 3:31PM PDT

<?php


function imitateMerge30(&$array1,&$array2,&$array3,&$array4,&$array5,&$array6,&$array7,&$array8,&$array9,&$array10,&$array11,&$array12,&$array13,&$array14,&$array15,&$array16,&$array17,&$array18,&$array19,&$array20,&$array21,&$array22,&$array23,&$array24,&$array25,&$array26,&$array27,&$array28,&$array29,&$array30) {
global $result30;

$result30=$array1;
foreach($array2 as $i) {$result30[] = $i;}
foreach($array3 as $i) {$result30[] = $i;}
foreach($array4 as $i) {$result30[] = $i;}
foreach($array5 as $i) {$result30[] = $i;}
foreach($array6 as $i) {$result30[] = $i;}
foreach($array7 as $i) {$result30[] = $i;}
foreach($array8 as $i) {$result30[] = $i;}
foreach($array9 as $i) {$result30[] = $i;}
foreach($array10 as $i) {$result30[] = $i;}
foreach($array11 as $i) {$result30[] = $i;}
foreach($array12 as $i) {$result30[] = $i;}
foreach($array13 as $i) {$result30[] = $i;}
foreach($array14 as $i) {$result30[] = $i;}
foreach($array15 as $i) {$result30[] = $i;}
foreach($array16 as $i) {$result30[] = $i;}
foreach($array17 as $i) {$result30[] = $i;}
foreach($array18 as $i) {$result30[] = $i;}
foreach($array19 as $i) {$result30[] = $i;}
foreach($array20 as $i) {$result30[] = $i;}
foreach($array21 as $i) {$result30[] = $i;}
foreach($array22 as $i) {$result30[] = $i;}
foreach($array23 as $i) {$result30[] = $i;}
foreach($array24 as $i) {$result30[] = $i;}
foreach($array25 as $i) {$result30[] = $i;}
foreach($array26 as $i) {$result30[] = $i;}
foreach($array27 as $i) {$result30[] = $i;}
foreach($array28 as $i) {$result30[] = $i;}
foreach($array29 as $i) {$result30[] = $i;}
foreach($array30 as $i) {$result30[] = $i;}

}

function imitateMerge15(&$array1,&$array2,&$array3,&$array4,&$array5,&$array6,&$array7,&$array8,&$array9,&$array10,&$array11,&$array12,&$array13,&$array14,&$array15) {
global $result15;

$result15=$array1;
foreach($array2 as $i) {$result15[] = $i;}
foreach($array3 as $i) {$result15[] = $i;}
foreach($array4 as $i) {$result15[] = $i;}
foreach($array5 as $i) {$result15[] = $i;}
foreach($array6 as $i) {$result15[] = $i;}
foreach($array7 as $i) {$result15[] = $i;}
foreach($array8 as $i) {$result15[] = $i;}
foreach($array9 as $i) {$result15[] = $i;}
foreach($array10 as $i) {$result15[] = $i;}
foreach($array11 as $i) {$result15[] = $i;}
foreach($array12 as $i) {$result15[] = $i;}
foreach($array13 as $i) {$result15[] = $i;}
foreach($array14 as $i) {$result15[] = $i;}
foreach($array15 as $i) {$result15[] = $i;}

}
function imitateMerge(&$array1,&$array2,&$array3) {
global $result;

$result=$array1;
foreach($array2 as $i) {
$result[] = $i;
}


foreach($array3 as $i) {
$result[] = $i;
}
}

$h3=0;
for($y=0; $y<200; $y=$y+2)
{
$newjump33[$h3]=$y;
$h3++;
}


for($v=0; $v<10; $v++)
{

$nnamenew="masterspeed_75_2_".$v.".txt";
$fpnew = fopen($nnamenew, "w");
for($j=0; $j<3; $j++)
{


$newjump=array_slice($newjump33,0,30);


$newjump2=array_slice($newjump33,30,30);


$newjump3=array_slice($newjump33,60,30);

for($t=0; $t<15; $t++)
{


$lines_ophold="";
for($x=0; $x<30; $x++)
{

$lines_ophold .=file_get_contents("pos2_part3_round3_part1_".$t."_".$newjump[$x].".txt",FILE_IGNORE_NEW_LINES)."\r\n";


}


$lines_op=array_keys(array_flip(explode("\r\n",$lines_ophold)));
$linesmaster1[$t]=$lines_op;

unset($lines_op);

}
$lines1=array_intersect($linesmaster1[0],$linesmaster1[1],$linesmaster1[2],$linesmaster1[3],$linesmaster1[4],$linesmaster1[5],$linesmaster1[6],$linesmaster1[7],$linesmaster1[8],$linesmaster1[9],$linesmaster1[10],$linesmaster1[11],$linesmaster1[12],$linesmaster1[13],$linesmaster1[14]);
unset($linesmaster1);


for($t=0; $t<15; $t++)
{


$lines_ophold="";
for($x=0; $x<30; $x++)
{





$lines_ophold .=file_get_contents("pos2_part3_round3_part1_".$t."_".$newjump2[$x].".txt",FILE_IGNORE_NEW_LINES)."\r\n";


}


$lines_op=array_keys(array_flip(explode("\r\n",$lines_ophold)));
$linesmaster1[$t]=$lines_op;

unset($lines_op);


}

$lines2=array_intersect($linesmaster1[0],$linesmaster1[1],$linesmaster1[2],$linesmaster1[3],$linesmaster1[4],$linesmaster1[5],$linesmaster1[6],$linesmaster1[7],$linesmaster1[8],$linesmaster1[9],$linesmaster1[10],$linesmaster1[11],$linesmaster1[12],$linesmaster1[13],$linesmaster1[14]);
unset($linesmaster1);


for($t=0; $t<15; $t++)
{


$nname="pos3_round3_part1_25_".$t.".txt";
$fp = fopen($nname, "w");


$lines_ophold="";
for($x=0; $x<30; $x++)
{




$lines_ophold .=file_get_contents("pos2_part3_round3_part1_".$t."_".$newjump3[$x].".txt",FILE_IGNORE_NEW_LINES)."\r\n";


}

$lines_op=array_keys(array_flip(explode("\r\n",$lines_ophold)));
$arr=implode("\r\n",$lines_op);
fwrite($fp,$arr."\r\n");
unset($lines_op);
fclose($fp);

}

$mainfile2="mymaster_6000_xxx_14of15a.php";
$command="php $mainfile2";
echo `$command`;


copy("masterspeed_round3.txt","masterspeed_round3_2.txt");
$lines3 = file("masterspeed_round3_2.txt",FILE_IGNORE_NEW_LINES);


$lines1=array_flip($lines1);

$lines2=array_flip($lines2);
$lines3=array_flip($lines3);
$result=array_intersect_key($lines1,$lines2,$lines3);
$result=array_flip($result);


$newarr=implode("\r\n",$result);

fwrite($fpnew,$newarr."\r\n");

unset($lines1);unset($lines2);unset($lines3);unset($result);unset($newarr);




}
fclose($fpnew);
}
?>

- Collapse -
Does not run here.
Apr 8, 2017 4:15PM PDT

$lines_ophold .=file_get_contents("pos2_part3_round3_part1_".$t."_".$newjump2[$x].".txt",FILE_IGNORE_NEW_LINES)."\r\n";

Where it reads files.

Is there a name for this math? Such as The Rothschild Slice?

- Collapse -
phpfiddle code online now
Apr 8, 2017 3:52PM PDT
- Collapse -
Now put this up on daniweb.
Apr 8, 2017 4:38PM PDT

You'll get more programmer eyeballs there.

And the code does not run there. Files missing?

- Collapse -
Answer
And now about PROFILING.
Apr 8, 2017 5:34PM PDT