簡體   English   中英

PHP Hashtable數組優化

[英]PHP Hashtable array optimisation

我制作了一個PHP應用程序,執行該程序大約需要0.0070秒。 現在,我添加了一個具有約2000個值的哈希表數組。 突然執行時間增加到約0.0700秒。 幾乎是先前值的10倍。

我嘗試注釋掉我在哈希表數組中搜索的部分(但仍保留定義數組)。 盡管如此,執行時間仍約為0.0500秒。

數組類似於:

$subjectinfo = array(
        'TPT753' => 'Industrial Training',
        'TPT801' => 'High Polymeric Engineering',
        'TPT802' => 'Corrosion Engineering',
        'TPT803' => 'Decorative ,Industrial And High Performance Coatings',
        'TPT851' => 'Project');

有什么方法可以優化這部分嗎?

我無法在Google應用程序引擎上運行此應用程序時使用數據庫,該引擎仍不支持php的JDO數據庫。

該應用程序提供了更多代碼:

function getsubjectinfo($name)
    {
        $subjectinfo = array(
        'TPT753' => 'Industrial Training',
        'TPT801' => 'High Polymeric Engineering',
        'TPT802' => 'Corrosion Engineering',
        'TPT803' => 'Decorative ,Industrial And High Performance Coatings',
        'TPT851' => 'Project');

    $name = str_replace("-", "", $name);
    $name = str_replace(" ", "", $name);
    if (isset($subjectinfo["$name"]))
        return "(".$subjectinfo["$name"].")";
    else
        return "";
   }

然后我在應用程序中使用以下語句2-3次:

echo $key." ".$this->getsubjectinfo($key)
function getsubjectinfo($name)
{
    $subjectinfo = array(
    'TPT753' => 'Industrial Training',
    'TPT801' => 'High Polymeric Engineering',
    'TPT802' => 'Corrosion Engineering',
    'TPT803' => 'Decorative ,Industrial And High Performance Coatings',
    'TPT851' => 'Project');
// ..
}

這樣, 每次調用函數時都會創建數組。 考慮在這里使用靜態變量

function getsubjectinfo($name)
{
    static $subjectinfo = array(
    'TPT753' => 'Industrial Training',
    'TPT801' => 'High Polymeric Engineering',
    'TPT802' => 'Corrosion Engineering',
    'TPT803' => 'Decorative ,Industrial And High Performance Coatings',
    'TPT851' => 'Project');
// ..
}

另外,您還可以使用SQLite-Database :)

更新:一種面向對象的方法

class MyClass {
    public static $subjectnames = array(
      'TPT753' => 'Industrial Training',
      'TPT801' => 'High Polymeric Engineering',
      'TPT802' => 'Corrosion Engineering',
      'TPT803' => 'Decorative ,Industrial And High Performance Coatings',
      'TPT851' => 'Project');

    public function getsubjectinfo($name) {
        $name = str_replace("-", "", $name);
        $name = str_replace(" ", "", $name);
        if (isset(self::$subjectnames["$name"]))
            return "(".self::$subjectnames["$name"].")";
        else
            return "";
    }
}

您可以嘗試使用整數作為表中的鍵。

“但是所有鍵不一定都具有TPT,它們可能是TOE362,AGS612等。我已經按升序對它們進行了排序,但想知道是否有幫助。”

將原始字符串散列為數字數據,然后將該輸出用作哈希鍵。 是的,每次訪問都涉及固定時間的罰款(用於額外的哈希),但是如果您的最終數據集足夠大,我懷疑這可能會超過讓PHP直接使用字符串鍵的性能。

如果所有其他方法均失敗,請使用C編寫對性能敏感的代碼,並將其編譯為PHP擴展。 不,更好,用C編寫整個應用程序。更好,對所有內容使用直接機器代碼。 或按照您希望的邏輯連接面包板! 參考: http//xkcd.com/378/

每天學習一課-在類構造函數中定義大型數組可以加快處理速度。

我將代碼修改為:

class myclass
{
    var $subjectnames = array();
    function myclass()
    {
        $this->subjectnames = array(
    'TPT753' => 'Industrial Training',
    'TPT801' => 'High Polymeric Engineering',
    'TPT802' => 'Corrosion Engineering',
    'TPT803' => 'Decorative ,Industrial And High Performance Coatings',
    'TPT851' => 'Project');
    }


function getsubjectinfo($name)
    {
        //$subjectinfo = array();
        $name = str_replace("-", "", $name);
        $name = str_replace(" ", "", $name);
        if (isset($this->subjectnames["$name"]))
            return "(".$this->subjectnames["$name"].")";
        else
            return "";
    }
}

我曾經讀過您可以通過序列化/反序列化優化這樣的靜態陣列配置。

在源代碼中插入數組的序列化版本。 這將是一個字符串。

使用unserialize(..)動態構建數組。

城市傳說說這可能節省一些解析時間。

您可以嘗試的另一件事是使用對象屬性而不是數組鍵。

$ obj-> TRV3463可能比數組訪問快。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM