KaptainKrayola

The Kaptain needs to trim every element of an array at the same time - any suggestions?

Yes, The Kaptain is posting in the correct forum he's actually doing stuff in

PHP

  nowadays.  Applause

<edit>

looking for a non-loop-over-every-element solution if possible

</edit>

hydra

array_map function -> write either a customised trim or use the standard trim()

perkiset

Hmmm... I think the fastest method I can imagine is to do a preg_replace, with your array being the third parameter. Consider:

$newArr = preg_replace('/^<>*(.*)<>*$/', '$1', $inputArr)

Although I'm not certain of my

regex

  syntax, I'm using a

regex

  that looks for anything inside of optional white space on the outsides of the string, and specifying the $1 back references (the inner goop) as the replacement. preg_match has the handy ability to take and return arrays as params, so in this case, that

regex

  will be applied to every element in $inputArr and $newArr will contain the results. Interestingly, you can supply arrays as parameters 1 and 2 as well, so you could have multiple search/replace instances happening against every element in an array... and since this is a single line of code and it's all in complied code nder the hood, this would be just ridiculously fast.

/p

hydra

Perk

Apologies in advance <- n00b

Just ran tests > 10000 keywords

<edit> 0 > 10,000 </edit>

Up to 10000 lines array_map seems 4/5x quicker than preg_match (in this instance)? for trimming whitespace at least


<?

php

 
$var=file_get_contents("keywords.txt");
$var=explode(" ", $var);
$new = array();
foreach ($var as $line=>$data)
{ array_push($new, $data); }
$result = array_map("randspace",$new);

function randspace($var)
{ return str_repeat(" ",rand(1,11)).$var.str_repeat(" ",rand(1,11));}

$start = microtime();
$res = array_map(trim,$new);
$finish = microtime();
echo "<br />";

echo "Array Map : ".($finish - $start);
echo "<br />";
$start = microtime();
$newArr = preg_replace('/^<>*(.*)<>*$/', '$1', $result);
$finish = microtime();
echo "Preg Match : ".($finish - $start);
?>


Array Map : 0.014735
Preg Match : 0.057089

<edit>
Original questions was fastest with non-loop - this is kind of 'loopy' unless put in method
</edit>

perkiset

NEVER apologize for a post like that... that's some of the best we've seen here - really nice work Hydra.

And that actually leads to a thought that Dirk had which I will be posting on tonight regarding benchmarking languages and techniques. I'll flesh that out tonight, but the fact that you did this right now is a fantastic segue.

Thanks!
/p

thedarkness

And a pretty damn good example of how to profile a

PHP

  app too.

Damn fine work hydra, haven't said G'day yet so...... please allow myself to introduce......... myself.

Cheers,
td

KaptainKrayola

w00t! Hydra is the Kaptain's new hero of the moment.  Thanks for that code - works like a charm.

Ok- back to the cave that is our rebuilding process.

The Kaptain will be back next time he wants code help Applause Applause

perkiset

Applause == CodeMooch  Applause

KaptainKrayola

You know the Kaptain well.  Why figure it out for yourself when you have THE CACHE?  Applause


Perkiset's Place Home   Politics @ Perkiset's