8

I need to store lots of two-dimensional arrays inside database and was not sure what to use: serialize or implode. So I did a few tests, to find out which one is working faster and came to the conclusion it was serialize:

Execution times: 1'000'000
Serialize: 1.4974119663239 seconds
Implode: 2.5333571434021 seconds
Explode: 4.0185871124268 seconds
Unserialize: 1.6835169792175 seconds 

So the question: Why is implode+explode so much slower then serialize+unserialize?

PS: I found this question already, but it is not exactly what I am asking.

Community
  • 1
  • 1
Peon
  • 7,902
  • 7
  • 59
  • 100

1 Answers1

1

My idea is that explode/implode operate on strings that's why, while serialize/unserialize output/input a string at the very ending/beginning. Probably the more strings you implode and the longer string you explode, the slower it is, have you tried?

Which to use I don't know, it depends if you later wish to handle output string, I think serialized string is more difficult to parse. But to store it in database or file I'd use serialize.

Voitcus
  • 4,463
  • 4
  • 24
  • 40
  • That sounds somehow reasonable. And yes, the larger the array, the longer implode/explode works. I already tested that too. – Peon Mar 05 '13 at 08:23
  • @DainisAbols I meant that it is not linear dependency. Is `implode`ing four strings exactly 2 times slower than imploding two strings? I guess it's more; and imploding eight strings would take more time than imploding two strings four times (all the strings should be the same length of course). – Voitcus Mar 05 '13 at 08:30
  • `Implode 2 elements: 1.8937180042267 seconds`, `Implode 4 elements: 2.4380650520325 seconds`, `Implode 8 elements: 3.6058769226074 seconds` Not exactly two times, but close to it, while serialize changes with the same progression type, but slower rate. – Peon Mar 05 '13 at 08:37
  • So I was wrong, imploding more elements is somehow faster. Then concatenating strings must be the weakest link. – Voitcus Mar 05 '13 at 08:58
  • @DainisAbols You could compare the implementation of the [**implode function**](https://github.com/php/php-src/blob/41433a52d69e81a8ed9ac8f357be239bd89ff3bb/ext/standard/string.c#L1129) with the [**serialize function**](https://github.com/php/php-src/blob/642721b38a9c5ebf336c81027c0dafd6f9246bd6/ext/standard/var.c#L711) – Anirudh Ramanathan Mar 05 '13 at 09:11