n00bie PHP Question

Status
Not open for further replies.
I ran each block of code thru 100 tests of 1000 iterations (100,000 total executions). Your code was 0.0087 seconds faster. Tests were run on an idle dual Xeon 2.4Ghz.

Mine (words.php)
Code:
<?php
$tests = 100;
$iterations = 1000;
$results = array();
for($y = 1; $y <= $tests; $y++)
{
 $time_start = microtime(true);
 for($z = 1; $z <= $iterations; $z++)
 {
  $source = explode("\n", file_get_contents("words.txt"));
  $words = array();
  for($i = 0; $i < count($source); $i++)
  {
   $line = explode(",",$source[$i]);
   $words[] = array($line[0],$line[1]);
  }
  shuffle($words);
  $word = $words[0];
  
  //echo $z.'. '.$word[0].', '.$word[1].'<br>';
 }
 $time_end = microtime(true);
 $time = $time_end - $time_start;
 $results[] = $time;
}
$avgtime = array_sum($results) / count($results);
echo '<h1>Executed '.$tests.' tests in an average of '.$avgtime.' seconds each.</h1>';

Yours (words2.php)
Code:
<?php
$tests = 100;
$iterations = 1000;
$results = array();
for($y = 1; $y <= $tests; $y++)
{
 $time_start = microtime(true);
 for($z = 1; $z <= $iterations; $z++)
 {
  $compare = file('words2.txt');
  
  //the number of rows in the file.
  $num = count($compare);
  
  //randomly generate a pair to display.
  $position = rand(0,$num);
  
  // to test whether $number is odd you could use the arithmetic
  // operator '%' (modulus) like this
  if($odd = $position % 2)
  {
   // $odd == 1.  We need to decrease our $position in the file.
   $second = $compare[$position];
   $position--;
   $first = $compare[$position];
  }
  else
  {
   // $odd == 0.  We are where we need to be in the file.
   $first = $compare[$position];
   $position++;
   $second = $compare[$position];
  }
  
  //echo $z.'. '.$first.', '.$second.'<br>';
 }
 $time_end = microtime(true);
 $time = $time_end - $time_start;
 $results[] = $time;
}
$avgtime2 = array_sum($results) / count($results);
echo '<h1>Executed '.$tests.' tests in an average of '.$avgtime2.' seconds each.</h1>';

Code:
<?php
require('words.php');
require('words2.php');
$diff = $avgtime - $avgtime2;
echo '<h1>Difference: '.$diff;
 


I ran each block of code thru 100 tests of 1000 iterations (100,000 total executions). Your code was 0.0087 seconds faster. Tests were run on an idle dual Xeon 2.4Ghz.

Changing to single quotes has got to be worth at least 0.0088 seconds.
 
Changing to single quotes has got to be worth at least 0.0088 seconds.


Damn! You're right. I had no idea that would make such a difference.

Now Rage's code is only 0.0017 seconds faster. Some further micro-optimizations have cut it down to 0.0014.
 
Damn! You're right. I had no idea that would make such a difference.

Now Rage's code is only 0.0017 seconds faster. Some further micro-optimizations have cut it down to 0.0014.

Not only that, the farther it's scaled the faster it'll be. :D

Yeah I know, I have been a real asshat trying to prove mine is a fraction of a second faster. But hey, fractions add up.
 
LOL, sorry I disappeared for a day. Glad I could give you guys a chance to compare your programming penises though!

I'm going to try some of these and see what works for me. I'll let you know.
 
Well, I've Audax's code up and it works perfectly! The only issue I had that took like 2 secs to figure out was that if any of your phrases in the text file have a "," in them it kinda goofs it up for obvious reasons.

Thanks, Audax!

Thanks also to Rage9! I'm going to try his solution as well since he spent the time on it. I'll let y'all know how it works for me also.

Much grass to everyone who helped out with this!
 
Yeah I know, I have been a real asshat trying to prove mine is a fraction of a second faster. But hey, fractions add up.

No worries. I wasn't trying to battle it out here, just having some coding fun... like playing golf in Perl.

BTW, you can shave some time using the explode(file_get_contents()) method over the basic file(). Not sure why but explode(file_get_contents()) was about 0.007 seconds faster over 1000 iterations.

I also micro-optimized Rage's code
Code:
  $compare = explode('\n', file_get_contents('words2.txt'));
  $position = rand(0, count($compare) - 1);
  $position -= $position % 2;
  $first = $compare[$position];
  $second = $compare[$position + 1];
 
No worries. I wasn't trying to battle it out here, just having some coding fun... like playing golf in Perl.

BTW, you can shave some time using the explode(file_get_contents()) method over the basic file(). Not sure why but explode(file_get_contents()) was about 0.007 seconds faster over 1000 iterations.

I also micro-optimized Rage's code
Code:
  $compare = explode('\n', file_get_contents('words2.txt'));
  $position = rand(0, count($compare) - 1);
  $position -= $position % 2;
  $first = $compare[$position];
  $second = $compare[$position + 1];

Good tip.

And now my code is bite sized, lol.
 
Status
Not open for further replies.