Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To optimize this a bit we need to remember that PHP has great overhead on parsing code as it is not compiled, so we need to do as much work ourselves for it as we can. You should always profile your CPU/memory sensitive code with xdebug+KCachegrind(for example) to see where PHP wastes most of it's time. With your code only 12% is spent on gmp_* functions calculations, most of the time is spent for code parsing.</p> <p>On my notebook(it is rather slow) my code runs 2.4 sec instead of 3.5 sec for your code, but for greater degrees the difference should be more noticeable (for example 19 power gives 19 vs 28 sec). It is not much, but it is some.</p> <p>I left comments inside code, but if you have some questions - feel free to ask. I used function creation to replace that 'for($i = 1; $i &lt; $n; $i++)' loop inside your main loop.</p> <p>Also, I think you should change type of your $period variable to GMP(and $period++ to gmp_* function) as it can be greater then maximum integer on your system.</p> <pre><code>function getPeriod() { $polynoms = array(16, 14, 13, 11); $highest = $polynoms[0]; $input = $highest - 1; //Delete first element of array - we don't need it anyway array_shift($polynoms); $polynoms_count = count($polynoms); //You always repeat gmp_pow(2, $input) and it's result is constant, //so better precalculate it once. $input_pow = gmp_pow(2, $input); //Start function creation. //If you don't use PHP accelerators, then shorter variable names //work slightly faster, so I replaced some of names //$perion-&gt;$r,$bit -&gt; $b, $lfsr -&gt; $l, $polynoms -&gt; $p $function_str = '$r=0;'; $function_str .= 'do{'; //Now we need to get rid of your loop inside loop, we can generate //static functions chain to replace it. //Also, PHP parses all PHP tokens, even ';' and it takes some time, //So, we should write as much one-liners as we can. $function_str .= '$b=gmp_xor($b=$l'; foreach ($polynoms AS $id =&gt; &amp;$polynom) { //You always repeat gmp_pow(2, $polynoms[$i]) and it's result is constant, //so better precalculate it once. $polynom = gmp_pow(2, $highest - $polynom); //We create our functions chain here if ($id &lt; $polynoms_count - 1) { $function_str.=',gmp_xor(gmp_div_q($l, $p[' . $id . '])'; } else { $function_str.=',gmp_div_q($l, $p[' . $id . '])'; } } //Close all brackets $function_str.=str_repeat(')', $polynoms_count); //I don't know how to optimize the following, so I left it without change $function_str.=';'; $function_str.='$l = gmp_or((gmp_div_q($l, 2)), (gmp_mul(gmp_and($b, 1), $i_p)));'; $function_str.='$r++;'; $function_str.='} while (gmp_cmp($l, 0x1));'; $function_str.='return $r;'; //Now, create our funciton $function = create_function('$l,$p,$i_p', $function_str); //Set begining states $lfsr = 0x1; $lfsr = gmp_init($lfsr, 16); //Run function $period = $function($lfsr, $polynoms, $input_pow); //Use result echo '&lt;br /&gt;period = ' . $period; return $period; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload