Jump to content

Recommended Posts

// Former method.
$temp = preg_split("/(\r?\n)/", $bigstring);
$temp_size = count($temp);
$n = 0;
while($n < $temp_size) {
$result[$temp[$n]] = $temp[$n + 1];
$n = $n + 2;
}

// Later method.
$temp = preg_split("/(\r?\n)/", $bigstring);
foreach(array_chunk($temp, 2) as $part){
$result[$part[0]] = $part[1];
}

 

To me it seems like the former is faster and saner, because it's similar to how I'd do it in C.

The later seems slower, because you convert array A into array B before it becomes array C.

 

However someone from ##php thought the former was slower and less sane, because "you're trying to optimize something php has already optimized for you, and you're doing it poorly."

I would GUESS the first one would be faster. A raw foreach will generally be faster than a for/while for iterating through an array, but the array_chunk will add extra memory requirements, and could be slow on it's own.

 

For huge arrays, I'd go with the former for sure.

For smaller arrays, I doubt there'd be enough difference to really matter...

My quick benchmarks seem to indicate the first one is faster, but only just barely.  I'm not sure how big your bigstring is, I tested using the contents of a 22MB file which is definitely a big string.  With such a big string a single run took a few seconds so I only ran it a few times rather than a few thousand.  Results:

Begin testing, num tests = 9

//Method 1
Total time: 27.806; tracked 1 points
Time track:
    Point: Method 1 end
        From start: 27.806
        From prev: 27.806

//Method 1(alt)
Total time: 3.093; tracked 1 points
Time track:
    Point: Method 1 (alt) end
        From start: 3.093
        From prev: 3.093


//Method 2
Total time: 28.880; tracked 1 points
Time track:
    Point: Method 2 ends
        From start: 28.880
        From prev: 28.880

//Method 2(alt)
Total time: 4.881; tracked 1 points
Time track:
    Point: Method 2 (alt) ends
        From start: 4.881
        From prev: 4.881

//Method 3
Total time: 3.474; tracked 1 points
Time track:
    Point: Method 3 ends
        From start: 3.474
        From prev: 3.474

 

Method 1(alt) and Method 2(alt) are the same as method 1 and 2, but using str_replace+explode rather than preg_split.  Method 3 is str_replace+explode and a for loop rather than the while loop.

 

<?php

require 'timer.php';
$bigstring = file_get_contents('bigstring');
$runCount=0;
$numTests=mt_rand(1,9);

echo "Begin testing, num tests = {$numTests}\r\n";
marktime(false, 'Method 1 begin');
for ($runCount=0; $runCount<$numTests; $runCount++){
unset($result);
$temp = preg_split("/(\r?\n)/", $bigstring);
$temp_size = count($temp);
$n = 0;
while($n < $temp_size) {
	$result[$temp[$n]] = $temp[$n + 1];
	$n = $n + 2;
}
}
marktime(true, 'Method 1 end');
echo "\r\n";

marktime(false, 'Method 1 (alt) begin');
for ($runCount=0; $runCount<$numTests; $runCount++){
unset($result);
$temp = explode("\n", str_replace(array("\r\n","\r"), "\n", $bigstring));
$temp_size = count($temp);
$n = 0;
while($n < $temp_size) {
	$result[$temp[$n]] = $temp[$n + 1];
	$n = $n + 2;
}
}
marktime(true, 'Method 1 (alt) end');
echo "\r\n";

markTime(false, 'Method 2 begin');
for ($runCount=0; $runCount<$numTests; $runCount++){
// Later method.
unset($result);
$temp = preg_split("/(\r?\n)/", $bigstring);
foreach(array_chunk($temp, 2) as $part){
	$result[$part[0]] = $part[1];
}
}
markTime(true, 'Method 2 ends');
echo "\r\n";

markTime(false, 'Method 2 (alt) begin');
for ($runCount=0; $runCount<$numTests; $runCount++){
// Later method.
unset($result);
$temp = explode("\n", str_replace(array("\r\n", "\r"), "\n", $bigstring));
foreach(array_chunk($temp, 2) as $part){
	$result[$part[0]] = $part[1];
}
}
markTime(true, 'Method 2 (alt) ends');
echo "\r\n";

marktime(false, 'Method 3 begin');
for ($runCount=0; $runCount<$numTests; $runCount++){
unset($result);
$temp = explode("\n", str_replace(array("\r\n","\r"), "\n", $bigstring));
for ($i=0,$len=count($temp); $i<$len; $i+=2){
	$result[$temp[$i]] = $temp[$i+1];
}
}
markTime(true, 'Method 3 ends');
echo "\r\n";

?>

 

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.