Visa ett inlägg
Oläst 2012-09-14, 22:56 #15
SimonPs avatar
SimonP SimonP är inte uppkopplad
Mycket flitig postare
 
Reg.datum: May 2006
Inlägg: 832
SimonP SimonP är inte uppkopplad
Mycket flitig postare
SimonPs avatar
 
Reg.datum: May 2006
Inlägg: 832
Appropå prestanda, i de flesta fall är ju OOP att föredra, men i de fall där man skall processa mkt data kan OOP ge en märkbar prestandaförsämring. För några år sedan gjorde jag tester mellan vanlig kod och OOP kod. I koden nedan krypteras en 1k-sträng 1k gånger, dvs totalt 1 megabyte data. OOP-varianten ger en enorm prestandaförsämring, flera hundra procent, testade även nu på en obelastad server med följande resultat:
Kod:
Non OOP Result: 1.6096091270447 seconds
OOP Result: 4.6408619880676 seconds
Kod:
<?php

// Create 1k string
$initString="";
while (strlen($initString)<1024) $initString .="abcdef0123456789";

/*
---------------------------------------------------
 Non-OOP test
---------------------------------------------------
*/
$time_start = microtime(true);
for ($i=0; $i<1024; $i++)
{
	Non_OOP_test($initString,false);
}
$time_end = microtime(true);
$time = ($time_end - $time_start);
echo "<pre>Non OOP Result: $time seconds\n";

// Verification
$encrypted	= Non_OOP_test($initString,true);
$decrypted	= Non_OOP_test($encrypted,true);
echo "Encrypted: $encrypted\n";
echo "Decrypted: $decrypted\n</pre>";

/*
---------------------------------------------------
 OOP test
---------------------------------------------------
*/
$time_start = microtime(true);
for ($i=0; $i<1024; $i++)
{
	OOP_test($initString,false);
}
$time_end = microtime(true);
$time = ($time_end - $time_start);
echo "<pre>OOP Result: $time seconds\n";

// Verification
$encrypted	= OOP_test($initString,true);
$decrypted	= OOP_test($encrypted,true);
echo "Encrypted: $encrypted\n";
echo "Decrypted: $decrypted\n</pre>";



function OOP_test($instr, $return)
{
	$arc4 = new ARC4();
	$arc4->init("pAsSwOrD");
	$arc4->process($instr,1024);
	if ($return) return $instr;
}


function Non_OOP_test($instr, $return)
{
	global $x, $y, $state, $statebackup;
	$x = $y = 0;
	$state = array();
	$statebackup =array();
	arc4_init("pAsSwOrD");
	arc4_process($instr,1024);
	if ($return) return $instr;
}

//-------------------------------------------------------------------------------
// ARC4 Encryption class in PHP
//--------------------------------------------------------------------------------
class ARC4
{
   private $state = array();
   private $statebackup = array ();
   private $x = 0;
   private $y = 0;

   function init($key)   // init key schedule
   {
      $len= strlen($key);
      for ($this->x = 0; $this->x < 256; $this->x++) $this->state[$this->x] = $this->x;

      $this->y = 0;
      for ($this->x = 0; $this->x < 256; $this->x++)
      {
         $this->y = ($this->y + $this->state[$this->x] + ord($key[$this->x % $len])) % 256;
         $temp = $this->state[$this->x];
         $this->state[$this->x] = $this->state[$this->y];
         $this->state[$this->y] = $temp;
      }
        
      // Skip the first 256 bytes to avoid some weakness
      $temp="";
      for ($this->x = 0; $this->x < 256; $this->x++) $temp[$this->x]=chr($this->x);
      $this->process($temp,256); 

      $this->x = $this->y = 0;
      $this->statebackup=$this->state;
    }

   function process(&$instr, $len)  // encrypt/decrypt raw ascii
   {
      for ($c= 0; $c < $len; $c++)
      {
         $this->x = ($this->x + 1) % 256;
         $this->y = ($this->y + $this->state[$this->x]) % 256;
         $temp = $this->state[$this->x];
         $this->state[$this->x] = $this->state[$this->y];
         $this->state[$this->y] = $temp;
         $instr[$c] = chr(ord($instr[$c]) ^ $this->state[($this->state[$this->x] + $this->state[$this->y]) % 256]);
      }
   }
}    
//--------------------------------------------------------------------------------------------------

	
function arc4_init($key)   // init key schedule
{
	global $x, $y, $state, $statebackup;
	
	$len= strlen($key);
   for ($x = 0; $x < 256; $x++) $state[$x] = $x;
   $y = 0;
   for ($x = 0; $x < 256; $x++)
   {
      $y = ($y + $state[$x] + ord($key[$x % $len])) % 256;
      $temp = $state[$x];
      $state[$x] = $state[$y];
      $state[$y] = $temp;
   }
     
   // Skip the first 256 bytes to avoid some weakness
   $temp="";
   for ($x = 0; $x < 256; $x++) $temp[$x]=chr($x);
   arc4_process($temp,256); 

   $x = $y = 0;
   $statebackup=$state;
}


function arc4_process(&$instr, $len)  // encrypt/decrypt raw ascii
{
	global $x,$y,$state;
		
   for ($c= 0; $c < $len; $c++)
   {
      $x = ($x + 1) % 256;
      $y = ($y + $state[$x]) % 256;
      $temp = $state[$x];
      $state[$x] = $state[$y];
      $state[$y] = $temp;
      $instr[$c] = chr(ord($instr[$c]) ^ $state[($state[$x] + $state[$y]) % 256]);
   }
}

?>
SimonP är inte uppkopplad   Svara med citatSvara med citat