Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51433 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 58306 invoked from network); 8 Feb 2011 12:55:46 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Feb 2011 12:55:46 -0000 Authentication-Results: pb1.pair.com header.from=ilia@prohost.org; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ilia@prohost.org; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain prohost.org designates 209.85.161.42 as permitted sender) X-PHP-List-Original-Sender: ilia@prohost.org X-Host-Fingerprint: 209.85.161.42 mail-fx0-f42.google.com Received: from [209.85.161.42] ([209.85.161.42:37610] helo=mail-fx0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 18/55-34020-05D315D4 for ; Tue, 08 Feb 2011 07:55:45 -0500 Received: by fxm11 with SMTP id 11so6179457fxm.29 for ; Tue, 08 Feb 2011 04:55:41 -0800 (PST) MIME-Version: 1.0 Received: by 10.223.103.8 with SMTP id i8mr7608375fao.47.1297169741681; Tue, 08 Feb 2011 04:55:41 -0800 (PST) Received: by 10.223.23.204 with HTTP; Tue, 8 Feb 2011 04:55:41 -0800 (PST) In-Reply-To: References: Date: Tue, 8 Feb 2011 07:55:41 -0500 Message-ID: To: Gustavo Lopes Cc: "internals@lists.php.net" Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Change default serialize precision from 100 to 17 From: ilia@prohost.org (Ilia Alshanetsky) +1 On Mon, Feb 7, 2011 at 8:26 PM, Gustavo Lopes wrot= e: > The default serialize precision is currently [1] set at 100. A little cod= e > inspection shows precision, in this case, takes the usual meaning of numb= er > of significant digits. > > Given that the implicit precision of a (normal) IEEE 754 double precision > number is slightly less than 16 digits [2], this is a serious overkill. P= ut > another way, while the mantissa is composed of 52 bits plus 1 implicit bi= t, > 100 decimal digits can carry up to 100*log2(10) =3D~ 332 bits of informat= ion, > around 6 times more. > > Given this, I propose changing the default precision to 17 (while the > precision is slightly less than 16, a 17th digit is necessary because the > first decimal digit carries little information when it is low). > > From my tests, this makes serialization and unserialization of doubles > around 3 times faster (counting the function calls to serialize/unseriali= ze, > plus a loop variable increment and stop condition check). It also makes t= he > serialization data. > > Crucially, from my tests, the condition that the variable stays the same > before and after serialization+unserialization still holds. The test > include, for little endian machines, verifies this. > > If no one objects, I'll change the default precision to 17. > > //run with php -d serialize_precision=3D17 > $numbers =3D array( > =A0 =A0 =A0 =A0"0000000000000000", //0 > =A0 =A0 =A0 =A0"2d431cebe2362a3f", //.0002 > =A0 =A0 =A0 =A0"2e431cebe2362a3f", //.0002 + 10^-Accuracy[.0002]*1.01 > =A0 =A0 =A0 =A0"0000000000001000", //2^-1022. (minimum normal double) > =A0 =A0 =A0 =A0"0100000000001000", //2^-1022. + 10^-Accuracy[2^-1022.]*1.= 01 > =A0 =A0 =A0 =A0"ffffffffffffef7f", //2^1024. (maximum normal double) > =A0 =A0 =A0 =A0"feffffffffffef7f", //2^1024. - 10^-Accuracy[2^1024.] > =A0 =A0 =A0 =A0"0100000000000000", //minumum subnormal double > =A0 =A0 =A0 =A0"0200000000000000", //2nd minumum subnormal double > =A0 =A0 =A0 =A0"fffffffffffff000", //maximum subnormal double > =A0 =A0 =A0 =A0"fefffffffffff000", //2nd maximum subnormal double > =A0 =A0 =A0 =A0"0000000000000f7f", //+inf > =A0 =A0 =A0 =A0"0000000000000fff", //-inf > ); > > foreach ($numbers as $ns) { > =A0 =A0 =A0 =A0$num =3D unpack("d", pack("H*", $ns)); $num =3D reset($num= ); > =A0 =A0 =A0 =A0echo "number: ", sprintf("%.17e", $num), "... "; > =A0 =A0 =A0 =A0$num2 =3D unserialize(serialize($num)); > =A0 =A0 =A0 =A0$repr =3D unpack("H*", pack("d", $num2)); $repr =3D reset(= $repr); > =A0 =A0 =A0 =A0if ($repr =3D=3D $ns) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0echo "OK\n"; > =A0 =A0 =A0 =A0else > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0echo "mismatch\n\twas: =A0 =A0$ns\n\tbecam= e: $repr\n"; > } > > > =A0 =A0[1]: http://lxr.php.net/opengrok/xref/PHP_TRUNK/main/main.c#451 > =A0 =A0[2]: http://en.wikipedia.org/wiki/Double_precision_floating-point_= format > -- > Gustavo Lopes > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >