在哪里可以放置多次访问常量值的数组? [英] Where to put arrays with constant value that will be accessed many times?

查看:95
本文介绍了在哪里可以放置多次访问常量值的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数组存储一些3D打印机命令的可能参数。我用它来检查命令是否合法。我很困惑我应该把这些数组放在哪里。这些数组只能在formatcheck函数中访问,并且该函数将被多次调用,因为有数千个命令要检查。我应该把这些作为变量的formatcheck函数还是作为私有静态变量放在格式检查函数的类的开头?

  {
$ Ms = array(82,83,84,104,106,107,109,140,190);
$ Gs = array(0,1,20,21,28,90,91,92);
$ Ts = array(0,1); ($($ this-> hasM()&& $ this-> hasNoXYZ()& amp; in_array($ this-> M,$ Ms))||($ this - > hasG()& in_array($ this-> G,$ Gs))||($ this-> hasT()&& $ this-> hasNoXYZ()& in_array($ this-> T,$ Ts)))
return false;
else
返回true;

或者:

  private static $ Ms = array(82,83,84,104,106,107,109,140,​​190); 
private static $ Gs = array(0,1,20,21,28,90,91,92);
private static $ Ts = array(0,1);
...
...
public function checkFileGcodeFormat()
{
if(!($ this-> hasM()&& $ this - > hasNoXYZ()& in_array($ this-> M,self :: $ Ms))||($ this-> hasG()& in_array($ this-> G, ($ this-> hasT()& amp; $ this-> hasNoXYZ()& in_array($ this-> T,self :: $ Ts)) )
返回false;
else
返回true;


解决方案

TL; DR :使用类常量来获得最佳性能(请参阅答案的最后)。



让我们看看不同版本的性能特征(以及为什么):

PHP 5



静态属性的数组是在编译时很快创建的,没有参与VM。虽然访问静态属性比访问普通变量慢一些,但仍然比在每次运行时重新创建数组快得多。



正常函数中的数组在运行时会重新创建在任何情况下,每次运行。并且在VM中运行时创建意味着每个元素都逐个添加到单独的操作码中,这意味着相当多的开销(特别是如果数组大于1-2个元素)。



PHP 7.0



正常函数的数组创建速度要快一些,因为一般情况下数组的创建速度会加快(在HashTable处理中进行优化)。如果它是所有常数值,它将缓存在内部常量值数组中,但在每次访问时都会重复。然而,直接高度专业化的复制操作显然比单独向PHP 5中的数组添加元素要快。



Opcache在内部将它们标记为IMMUTABLE,这允许直接访问[所以你得到全速opcache]。 (另请参阅 https://blog.blackfire.io/php- 7-performance-improvements-immutable-arrays.html



PHP 7.1



数组本地总是缓存在内部常量值数组中,具有写时复制语义。

现在使用静态属性比查找静态属性的性能要慢简单地写入一个变量。 [直接访问一个变量没有额外的开销。]




另外请注意,由于PHP 5.6可以声明(类)具有数组值的常量。 PHP 7.1允许直接替换同一类的类常量,并将数组直接添加到内部常量值数组中,以便直接与in_array一起使用。



I。最快的代码是(至少7.1):

pre code私有常量Ms =数组(82,83,84,104,106 ,107,109,140,​​190);
private const Gs = array(0,1,20,21,28,90,91,92);
private const Ts = array(0,1);
...
...
public function checkFileGcodeFormat()
{
if(!($ this-> hasM()&& $ this - > hasNoXYZ()& in_array($ this-> M,self :: Ms))||($ this-> hasG()& in_array($ this-> G,self Gs))||($ this-> hasT()&& $ this-> hasNoXYZ()& amp; in_array($ this-> T,self :: Ts)))
返回false;
else
返回true;
}


I have some arrays storing the possible parameters for some 3D printer commands. I use this to check if the command is legal. I am confused about where I should put these arrays. These arrays will only be accessed in the formatcheck function, and the function will be called many times as there are thousands of commands to check. Should I put these in the formatcheck function as variables or at the beginning of the class the formatcheck function is in, as private static variables?

public function checkFileGcodeFormat()
{
    $Ms = array(82, 83, 84, 104, 106, 107, 109, 140, 190);
    $Gs = array(0, 1, 20, 21, 28, 90, 91, 92);
    $Ts = array(0, 1);
    if (! ($this->hasM() && $this->hasNoXYZ() && in_array($this->M, $Ms)) || ($this->hasG() && in_array($this->G, $Gs)) || ($this->hasT() && $this->hasNoXYZ() && in_array($this->T, $Ts)) )
        return false;
    else
        return true;
}   

or:

private static $Ms = array(82, 83, 84, 104, 106, 107, 109, 140, 190);
private static $Gs = array(0, 1, 20, 21, 28, 90, 91, 92);
private static $Ts = array(0, 1);
...
...
public function checkFileGcodeFormat()
{
    if (! ($this->hasM() && $this->hasNoXYZ() && in_array($this->M, self::$Ms)) || ($this->hasG() && in_array($this->G, self::$Gs)) || ($this->hasT() && $this->hasNoXYZ() && in_array($this->T, self::$Ts)) )
        return false;
    else
        return true;
}

解决方案

TL;DR: Use a class constant for maximum performance (see at the end of the answer).

Let's look at the performance characteristics of the different versions (and why):

PHP 5

Arrays in static properties are created at compile time, very quickly, without involvement of the VM. Accessing static properties though is a bit slower than accessing normal variables, but still much faster than recreating the array on every run.

Arrays in normal functions get re-created at run-time with every run, in any case. And creation at run-time in VM means that every element is added one-by-one, in individual opcodes, which means quite a bit of overhead (especially if the array is larger than just 1-2 elements).

PHP 7.0

Arrays in normal functions [in general] are created a bit faster due to array creation in general being sped up (optimizations in HashTable handling). If it's all constant values, it's cached in the internal constant values array, but duplicated upon each access. However doing a direct highly specialized copying action is obviously faster than adding elements one-by-one to the array like in PHP 5.

Opcache is marking them as IMMUTABLE internally, which allows direct access [so you get full speed with opcache]. (See also https://blog.blackfire.io/php-7-performance-improvements-immutable-arrays.html)

PHP 7.1

Arrays are natively always cached in the internal constant values array, with copy-on-write semantics.

Now using a static property is slower as looking up a static property is less performant than a simple write to a variable. [Direct access to a variable has no extra overhead.]


Also note that since PHP 5.6 you can declare (class) constants with the value of an array. PHP 7.1 allows direct substitution of class constants of the same class and will add the array directly to the internal constant values array for direct usage with in_array.

I.e. the fastest code is (with 7.1 at least):

private const Ms = array(82, 83, 84, 104, 106, 107, 109, 140, 190);
private const Gs = array(0, 1, 20, 21, 28, 90, 91, 92);
private const Ts = array(0, 1);
...
...
public function checkFileGcodeFormat()
{
    if (! ($this->hasM() && $this->hasNoXYZ() && in_array($this->M, self::Ms)) || ($this->hasG() && in_array($this->G, self::Gs)) || ($this->hasT() && $this->hasNoXYZ() && in_array($this->T, self::Ts)) )
        return false;
    else
        return true;
}

这篇关于在哪里可以放置多次访问常量值的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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