Prismatic Posted February 7, 2009 Share Posted February 7, 2009 My server sucks. 100k call * 10 benchmarking: Crayon Test Fatal error: Maximum execution time of 30 seconds exceeded in benchmark.php(19) : eval()'d code on line 1 Quote Link to comment https://forums.phpfreaks.com/topic/144139-string-to-array/page/2/#findComment-756455 Share on other sites More sharing options...
corbin Posted February 7, 2009 Share Posted February 7, 2009 Wow.... Xeons own my processor. 2.8ghz clock speed with 2 cores (PHP only uses 1 core if I remember correctly), but it takes 15 and 31 seconds. (I would link you to my phpsysinfo page, but it's a personal computer that I would rather not show.....) The interesting parts: Processors 2 Model Intel® Pentium® D CPU 2.80GHz CPU Speed 2.8 GHz BUS Speed 200 Mhz Cache Size 1024 Yeah, 200Mhz bus speed. And yeah, Pentium D. I hate Pentium D, by the way. I'm quite surprised by how well yours scales. To start with, I've always had stuck in my head that eval is slow. Guess not ;p. Also, I would have thought the implode would be slow, but I guess you only call it once, whereas my code has to make x iterations. Also, in the deep depths of PHP's insides, I have no idea how memory management for arrays is handled. For all I know, it could be taxing time wise to keep reallocating memory for arrays. Quote Link to comment https://forums.phpfreaks.com/topic/144139-string-to-array/page/2/#findComment-756536 Share on other sites More sharing options...
sasa Posted February 7, 2009 Share Posted February 7, 2009 <?php $test = 'Paths.Images.Icons'; $out = array(); foreach (array_reverse(explode('.',$test)) as $v) $out = array($v => $out); print_r($out); ?> Quote Link to comment https://forums.phpfreaks.com/topic/144139-string-to-array/page/2/#findComment-756580 Share on other sites More sharing options...
.josh Posted February 7, 2009 Share Posted February 7, 2009 look at sasa trying to mash a foreach into one line. That's like a fat lady trying to wear spandex! For shame.... Here's your averages using same benchmark. Looks like corbin's deals with smaller strings slightly faster, but yours scales slightly better than his. Sasa Test 3 layers 0.534427165985 0.534028053284 0.534690856934 0.536725997925 0.536411046982 0.536748886108 0.535420894623 0.533998012543 0.539067983627 0.536219120026 Sasa Test 10 layers 1.08157396317 1.08768081665 1.0953938961 1.08387899399 1.08178806305 1.07894706726 1.07972192764 1.10600399971 1.08545613289 1.08323502541 Sasa Test 100 layers 8.03103399277 8.07867789268 7.99266791344 8.03767514229 8.00496792793 8.02353405952 8.04677987099 8.03564500809 8.07798290253 8.08749604225 Quote Link to comment https://forums.phpfreaks.com/topic/144139-string-to-array/page/2/#findComment-756597 Share on other sites More sharing options...
corbin Posted February 7, 2009 Share Posted February 7, 2009 I'm perfectly happy being beaten by that genius snippet of code.... Never would have thought of that. lol. Quote Link to comment https://forums.phpfreaks.com/topic/144139-string-to-array/page/2/#findComment-756613 Share on other sites More sharing options...
.josh Posted February 7, 2009 Share Posted February 7, 2009 waveMagicWand(); // <--no need to pass anything, it just knows. Magic Wand time: 0.00 0.00 -1 <--they said they needed it done by yesterday. Blam! -1 0.01 <-- only because you blinked 0.00 Quote Link to comment https://forums.phpfreaks.com/topic/144139-string-to-array/page/2/#findComment-756614 Share on other sites More sharing options...
uniflare Posted February 7, 2009 Share Posted February 7, 2009 I'm interested to see how much faster this would be: (Since putting functions if loop statements gets executed each time ) <?php $test = 'Paths.Images.Icons'; $rVerse = array_reverse(explode('.',$test)); foreach ($rVerse as $v) $out = array($v => $out); print_r($out); ?> maybe slightly faster? Quote Link to comment https://forums.phpfreaks.com/topic/144139-string-to-array/page/2/#findComment-756648 Share on other sites More sharing options...
mister5317 Posted February 8, 2009 Author Share Posted February 8, 2009 <?php $test = 'Paths.Images.Icons'; $out = array(); foreach (array_reverse(explode('.',$test)) as $v) $out = array($v => $out); print_r($out); ?> Wow! That is genus and so simple! Also, I compared the two slightly different functions, see below. <?php // Sasa's Function function strToNestedArray($string) { $out = array(); foreach (array_reverse(explode('.',$string)) as $v) $out = array($v => $out); return $out; } // uniflare Function function str_to_nested_array($string) { $rVerse = array_reverse(explode('.',$string)); foreach ($rVerse as $v) $out = array($v => $out); return $out; } $str = "path.to.something.that.is.really.neat"; echo "100k call * 10 benchmarking: <br/><br/>"; echo "Sasa's Test<br/>"; for ($y = 0; $y < 10; $y++) { $start = microtime(true); for ($x = 0; $x < 100000; $x++) { $structure = strToNestedArray($str); } $time = microtime(true) - $start; echo $time . "<br/>"; } echo "<br/>uniflare 's Test<br/>"; for ($y = 0; $y < 10; $y++) { $start = microtime(true); for ($x = 0; $x < 100000; $x++) { $array = str_to_nested_array($str); } $time = microtime(true) - $start; echo $time . "<br/>"; } Here are the results: 100k call * 10 benchmarking: Sasa's Test 0.581806898117 0.576666116714 0.580965995789 0.581721782684 0.580982923508 0.579081058502 0.579075813293 0.578878879547 0.578855037689 0.580253124237 uniflare's Test 0.655313968658 0.654915094376 0.655946016312 0.654715061188 0.655534029007 0.662548065186 0.647897958755 0.648191213608 0.6495449543 0.650224924088 I really appreciate everyone's help and snippets. Thanks everyone! Quote Link to comment https://forums.phpfreaks.com/topic/144139-string-to-array/page/2/#findComment-757252 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.