PHP哈希表数组优化
我制作了一个PHP应用程序,执行时间约为0.0070秒。现在,我添加了一个大约有2000个值的哈希表数组。突然,执行时间增加到约0.0700秒。几乎是之前数值的10倍 我试着注释掉我在哈希表数组中搜索的部分(但数组仍然是左定义的)。尽管如此,执行时间仍约为0.0500秒 数组类似于:PHP哈希表数组优化,php,optimization,hashtable,Php,Optimization,Hashtable,我制作了一个PHP应用程序,执行时间约为0.0070秒。现在,我添加了一个大约有2000个值的哈希表数组。突然,执行时间增加到约0.0700秒。几乎是之前数值的10倍 我试着注释掉我在哈希表数组中搜索的部分(但数组仍然是左定义的)。尽管如此,执行时间仍约为0.0500秒 数组类似于: $subjectinfo = array( 'TPT753' => 'Industrial Training', 'TPT801' => 'High Polymeric
$subjectinfo = array(
'TPT753' => 'Industrial Training',
'TPT801' => 'High Polymeric Engineering',
'TPT802' => 'Corrosion Engineering',
'TPT803' => 'Decorative ,Industrial And High Performance Coatings',
'TPT851' => 'Project');
有没有办法优化这个部分
我不能使用数据库,因为我在Google app engine上运行这个应用程序,它仍然不支持用于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)
您可以尝试使用整数作为表中的键 “但并非所有键都必须有TPT,它们可能是TOE362、AGS612等。我已按升序对它们进行了排序,但不知道这是否有帮助。” 将原始字符串散列为数字数据,并将该输出用作散列键。是的,每次访问都会有一个固定的时间惩罚(对于额外的散列),但是如果您的最终数据集足够大,我怀疑这可能比让PHP直接使用字符串键要好 如果所有这些都失败了,用C编写性能敏感代码,并将其编译为PHP扩展。不,更好的是,用C语言编写整个应用程序。更好的是,使用直接的机器代码。或者用你想要的逻辑连接一个试验板!参考: 这样,每次调用函数时都会创建数组。考虑这里使用静态变量< /P>
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数据库:)
更新:一种面向对象的方法
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 "";
}
}
今天学到的一课-在类构造函数中定义大型数组可以加快速度 我将代码修改为:
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(..)动态构建阵列 Urban legend表示,这可能会节省一些解析时间 您可以尝试的另一件事是使用对象属性而不是数组键
$obj->TRV3463可能比数组访问速度快。是否在程序中只构建一次此数组?是的。这个数组将持续数周…@hiprakhar:这不是我要问的。@jon我在我的项目中只声明过一次这个数组。在应用生命周期内在此数组内搜索2次。如果每个键中都有TPT,请尝试使用不带TPT的整数索引创建数组。整数索引将起作用faster@kingchurch谢谢我使用构造函数来定义数组,这几乎是一样的。没有看到任何构造函数,它也不会改变任何东西:
$subjectinfo
是一个局部变量,每次调用它时都会在方法中创建。谢谢漫画。。喜欢:)
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 "";
}
}