Dart中整数变量的大小是多少 [英] What is the size of an integer variable in Dart

查看:82
本文介绍了Dart中整数变量的大小是多少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Dart网站上,他们说整数大小是64位,这是整数的最大大小,还是整数,即使该整数是一个小数(例如12)?

In Dart site they said that the integer size is 64 bit , is this the max size of an integer, or this is the size of any integer even if the integer is a small number such as 12?

如果每个整数大小均为64位,这会影响应用程序的性能吗?

If every integer size is 64 bit , does this affect the performance of the application?

推荐答案

答案是:取决于".

首先,如果将Dart编译为JavaScript,则所有数字均为JavaScript数字,这意味着Dart double .即使是整数,它们也恰好是非分数双精度数.这意味着您最多具有53位精度.

First of all, if you compile Dart to JavaScript, all numbers are JavaScript numbers, which means Dart doubles. Even the integers, they just happen to be non-fractional doubles. That means that you have at most 53 bits of precision.

本机Dart数字是64位整数.如果VM可以从中得到好处,则可以在内部将它们表示为较小的数字.

The native Dart numbers are 64-bit integers. The VM may represent them as smaller numbers internally if it can see an advantage in that.

在64位VM上,所有指针都是64位,因此您不能存储比堆中小(至少在类型数据列表之外)的任何东西.不过,如果您的值是一个 63 位有符号整数,则VM可以将其直接存储在指针中(所谓的小整数"或"smi"),否则必须分配一个堆对象存储64位,因此它仍然可以判断整数与堆指针之间的区别.这对于垃圾收集很重要.

On a 64-bit VM, all pointers are 64 bits, so you can't store anything smaller than that in the heap (at least outside of typed-data lists). Still, if your value is a 63-bit signed integer, the VM can store it directly in a pointer (a so-called "small integer" or "smi"), otherwise it has to allocate a heap object to store the 64 bits, so that it can still tell the difference between an integer and a heap pointer. That's important for garbage collection.

在32位VM上,所有指针都是32位,并且如果您的整数是 31 位有符号整数,则可以将其存储在指针中,否则它将成为堆对象.

On a 32-bit VM, all pointers are 32 bits, and if your integer is a 31-bit signed integer, it can be stored in the pointer, otherwise it becomes a heap object.

用于存储数字.对于函数内部的局部变量,VM可以选择对值取消装箱,在堆栈上存储纯整数,而不是堆号或带标签的小整数.如果编译器知道您仅使用16位整数,则编译器可以(理论上)仅使用16位整数.实际上,小于32位的任何东西可能效率较低,甚至在64位平台上也是如此.

That's for storing numbers. For a local variable inside a function, the VM can choose to unbox the value, storing plain integers on the stack instead of heap-numbers or tagged small integers. If the compiler knows that you only use 16-bit integers, it can (theoretically) use only 16 bits for it. In practice, anything smaller than 32 bits is probably less efficient, and perhaps even that on 64-bit platforms.

这些优化是出于性能原因而发生的,所以是的,它会影响性能,并且编译器会尝试避免不必要的开销.

These optimizations happen for performance reasons, so yes, it affects performance, and the compiler tries to avoid unnecessary overheads.

这是当前的实现.只要您在运行程序时不知道区别就可以随时更改.

That's the current implementation. It an change at any time as long as you can't tell the difference when running your program.

这篇关于Dart中整数变量的大小是多少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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