生成数千个对象类型时的 PHP OO 性能

发布于 2024-12-10 14:39:25 字数 1818 浏览 0 评论 0原文

我一直在阅读 Matt Zandstra PHP 面向对象设计和模式并且非常喜欢一个想法,但想知道当您创建数百甚至数千个模式时它如何影响 PHP 性能 他们。

您有一张由数千种不同类型的图块组成的地图(就像在文明中一样)。 您有一个通用图块,然后其他图块可能是 10 种不同类型之一。

他基本上是说,您创建一个基本图块,然后让其他图块继承以下属性:

abstract class Grass {

    public movementSpeed = 1;

    public function getMovementSpeed () {
        return this.movementSpeed;
    }        

}

class Hill extends Grass {
    public movementSpeed = 0.5;
}

class Mountain extends Grass {
    public movementSpeed = 0.3;
}

class Lake extends Grass {
    public movementSpeed = 0.6;

    public function seaMonster () {
        // Kill between 0.2 - 0.4x of units with sea monster 
    }

}

您拥有构成此地图的所有不同类型的地形(在第一代时),然后循环遍历数据库来构建它就像:

ID | TYPE
----------------- 
1 | grass
2 | mountain
3 | mountain
4 | grass
5 | lake
6 | grass

所以我想我的问题是......这与性能有何关系? PHP 是否将每个图块存储为一个大对象,还是仅包含对对象定义的引用?

我只是在想,当它是一张充满 1,000 个正方形的地图并且我正在循环并做其他爵士乐的事情时,它会变得非常慢,因为我一直认为面向对象编程非常庞大。

将其程序化,然后循环遍历方块并运行函数来决定这些方块会发生什么会更快吗?

只是想知道使用这样的 OO 设计/模式时总体性能如何?

编辑:我所说的面向对象和程序循环的意思是这样的:

// Procedural

$tiles = array();

// Loop through DB of tiles
$tiles[] = $row->type;

$speed = getSpeed($tiles[0]);

// OO
$map = new Map;

// Loop through DB of tiles
switch ($row->type) {
   case 1:
       $map->addTile(new Plains);
       break;
   case 2: 
       $map->addTile(new Mountain);
       break;
}

$tile = $map->getTile(0);
$speed = $tile->getMovementSpeed();

如您所见,面向对象方法确实很有意义,您正在用图块填充地图,但是对于 2 座山,是否存在任何重复或当您调用时它是否只是引用了类蓝图来说明它在做什么。

我想它只是调用构造方法(如果有的话),然后当您需要它们时,您手头上就有它的方法,所以它非常快,对吗?

谢谢,多姆

I've been reading the Matt Zandstra PHP Object Oriented Designs and Patterns and quite liked an idea but was wondering how it effects PHP Performance when you create hundreds maybe thousands of them.

You have a map consisting of thousands of different type of tiles (like in civilization).
You have a generic tile and then the other tiles may be one of 10 different types.

He was basically saying that you create a Basic tile and then have the other tiles inherit the properties like:

abstract class Grass {

    public movementSpeed = 1;

    public function getMovementSpeed () {
        return this.movementSpeed;
    }        

}

class Hill extends Grass {
    public movementSpeed = 0.5;
}

class Mountain extends Grass {
    public movementSpeed = 0.3;
}

class Lake extends Grass {
    public movementSpeed = 0.6;

    public function seaMonster () {
        // Kill between 0.2 - 0.4x of units with sea monster 
    }

}

You have all of the different types of terrains that make up this map (upon first generation) and then loop through the database to build it up like:

ID | TYPE
----------------- 
1 | grass
2 | mountain
3 | mountain
4 | grass
5 | lake
6 | grass

So I guess my question is... how does this go with performance? Does PHP store every tile as a big object or does it just contain a reference to the object definition?

I'm just thinking when its a map full of 1,000s of squares and I'm looping through and doing other jazzy stuff, will it be crazy slow as I always thought Object Oriented programming was quite bulky.

Would it be much quicker to have it procedural then loop through the squares and run functions to decide what happens with these?

Just wondering in general what performance is like when using an OO Design/Pattern like this?

Edit: What I mean by OO and Procedural looping is something like:

// Procedural

$tiles = array();

// Loop through DB of tiles
$tiles[] = $row->type;

$speed = getSpeed($tiles[0]);

// OO
$map = new Map;

// Loop through DB of tiles
switch ($row->type) {
   case 1:
       $map->addTile(new Plains);
       break;
   case 2: 
       $map->addTile(new Mountain);
       break;
}

$tile = $map->getTile(0);
$speed = $tile->getMovementSpeed();

As you can see, the OO method really makes sense, you're populating a map with tiles but for 2 mountains is there any sort of duplication or when you come to call it, does it just reference the class blueprint as to what its doing.

I guess it just calls the construct method (if there is one) and then you have it's methods on hand when you need them so its pretty quick right?

Thanks, Dom

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文