在PHP中获取浮动大小 [英] Getting Size of Float in PHP

查看:96
本文介绍了在PHP中获取浮动大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一个浮点数在PHP5中占用了多少内存.我已经阅读到 memory_get_usage()不太可靠.

I would like to check how much memory a floating point number occupies in PHP5. I have read that memory_get_usage() is not very reliable.

我用它来尝试确定浮点数的大小,每当我创建一个新的浮点数时,内存使用量就会增加168个字节.我认为这似乎太大了,所以我的问题是-这个数字有多可靠?有没有更好的方法来估算浮点数的大小?

I have used it to try and determine the size of floats, and every time that I create a new floating-point number the memory usage increments by 168 bytes. I thought that this seems too large, so my question is - how reliable is this figure? Is there a better way to approximate the size of floating-point numbers?

我的服务器的操作系统是Ubuntu 14.04.

My server's OS is Ubuntu 14.04.

推荐答案

memory_get_usage() 返回PHP用于存储程序变量及其内部布尔值的内存量.

memory_get_usage() returns the amount of memory used by PHP to store the variables of your program and its internal bookeeping of these variables.

memory_get_usage()返回的值与用于存储脚本处理的实际值的内存量之间没有直接链接.

There is no direct link between the value returned by memory_get_usage() and the amount of memory used to store the actual values handled by your script.

由于PHP变量没有类型,因此每个变量都使用允许其存储任何类型值的数据结构.这意味着,即使对于bool值的1位有效载荷,PHP也会占用大量内存(可能是您测量的168个字节).

Because the PHP variables don't have a type, each variable uses a data structure that allows it to store values of any type. This means, even for the 1-bit payload of a bool value, PHP uses a lot of memory (probably those 168 bytes you measured).

当值是数组的一部分时,该数量甚至更大,因为数组是一种复杂的类型,混合了数组,哈希和双链表的结构和功能.为了提供这些不同数据结构的功能,数组类型在其存储的对象之间保持链接,并且这些链接也使用内存.

The amount is even bigger when the value is part of an array because an array is a complex type that mixes the structure and functionality of arrays, hashes and double-linked lists. In order to provide the functionality of these different data structures, the array type keeps links between the objects it stores and these links also use memory.

PHP 7改变了内部存储数据的方式,并且平均而言,它将使用的内存量减少到PHP 5使用的内存的一半左右.

PHP 7 changed the way it stores its data internally and, in average, it reduced the amount of used memory to about one half of the memory used by PHP 5.

但是,如果您想知道用于存储实际浮点值的字节数(以便知道其可能的值范围fe),则该信息可在

However, if you want to know how many bytes are used to store the actual float value (in order to know its range of possible values, f.e.), the information is available in the documentation page of the float type:

float的大小取决于平台,尽管通常值(64位IEEE格式)的最大值为〜1.8e308,精度约为14个十进制数字.

The size of a float is platform-dependent, although a maximum of ~1.8e308 with a precision of roughly 14 decimal digits is a common value (the 64 bit IEEE format).

我认为依赖于平台"是指在32位平台(float的传统大小)上使用4个字节,在64位及更大平台(传统的double)上使用8个字节.

I suppose "platform-dependent" means it uses 4 bytes on 32-bit platforms (the traditional size for float) and 8 bytes on 64-bit and larger platforms (the traditional double).

这篇关于在PHP中获取浮动大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆