Jump to content

String to Array


mister5317

Recommended Posts

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

I'm interested to see how much faster this would be: (Since putting functions if loop statements gets executed each time :P)

 

 <?php
$test = 'Paths.Images.Icons';
$rVerse = array_reverse(explode('.',$test));

foreach ($rVerse as $v) $out = array($v => $out);

print_r($out);
?> 

 

maybe slightly faster? :P

Link to comment
Share on other sites

<?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!

Link to comment
Share on other sites

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.