By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,751 Members | 1,158 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,751 IT Pros & Developers. It's quick & easy.

Serialize / Unserialize

P: n/a
Some have suggested that using serialize()
and unserialize is faster than reading/writing
an array to disk as a simple text file using
$array = file('numbers.txt');

Can anyone justify this?

It seems to me that there is some overhead
associated with calling unserialize().

Is this actually faster than using file()?

Regards,

Andrew

Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Mon, 21 Jun 2004 13:23:02 +0000, Andrew wrote:
Some have suggested that using serialize()
and unserialize is faster than reading/writing
an array to disk as a simple text file using
$array = file('numbers.txt');

Can anyone justify this?

It seems to me that there is some overhead
associated with calling unserialize().

Is this actually faster than using file()?

Regards,

Andrew


I would guess that serialize/unserialize is faster because it
involves memory-only operations whereas file() involves disk access. Disk
access is *always* slower than memory access!

Just a hypothesis on the reason, though. No hard facts to back it up.

--
Jeffrey D. Silverman | jeffrey AT jhu DOT edu
Website | http://www.wse.jhu.edu/newtnotes/

Jul 17 '05 #2

P: n/a

"Andrew" <an****@nowherenohow.com> wrote in message
news:Wi*****************@newsread1.news.pas.earthl ink.net...
Some have suggested that using serialize()
and unserialize is faster than reading/writing
an array to disk as a simple text file using
$array = file('numbers.txt');

Can anyone justify this?

It seems to me that there is some overhead
associated with calling unserialize().

Is this actually faster than using file()?


I was the one who said it so I'll explain my rationale. Serialize is
marginally faster than file because the format is optimized for easy parse.
Take the following array:

$a = array("This is a test", "Hello world");

Run it through serialize() and you get:

a:2:{i:0;s:14:"This is a test";i:1;s:11:"Hello world";}

How look at this carefully. "a:2" indicates an array with two elements. So
now the start PHP knows how large the array is going to be, and can thus
allocate enough memory from the getgo to hold the entire array. In case of
file() the function does not know how many lines there will be, so it has to
reallocate memory continually as more lines are read.

Now look at "s:14". That stands for a string with a length of 14. Again, PHP
knows how long the string is going to be before it reads the data, so it can
allocate a buffer of just the right size. Moreover, since it knows so many
characters the string takes up in the input data, it can jump ahead to the
next variable without having to examine each characters in the string. In
contrast file() needs to look at every character to know where a newline
starts.

Using serialize also means you don't have to deal with the carriage returns
at the end of every element.
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.