简体   繁体   English

PHP中的高性能OOP

[英]High(er) performance OOP in PHP

pretty much a question in regards to performance when working with objects. 在处理对象时,有关性能的问题非常多。 Currently using ZF2 and trying to replicate things in my game. 当前正在使用ZF2,并尝试复制游戏中的内容。

I have a map with terrains on them. 我有一张上面有地形的地图。 Each terrain has a name and a description. 每个地形都有一个名称和描述。

The description could be something between 200-500 words long. 描述的长度可能在200-500字之间。 If I'm building a map hundreds, thousands of these objects then in terms of performance, where would I be better off putting the description. 如果要构建成百上千个这样的对象的地图,那么就性能而言,最好在其中添加描述。

If it was a property like so: 如果是这样的属性:

public $description = 'Blah blah blah.... 500 chars long';

Would it create say.... 1,000 x 500 byte descriptions when I'm looping through and putting these objects onto a map or does PHP optimise and say, the description won't be created until its requested through $obj->description . 当我遍历并将这些对象放到地图上时,它会创建1000 x 500字节的描述吗?或者PHP优化并说,直到通过$obj->description请求描述之后,描述才会创建。 If that's the case then if it was to be in method like..... 如果是这样的话,那么就应该使用.....这样的方法

public getDescription () { 
    return "Blah blah blah.... 500 chars long"; 
} 

then is this using less memory because it doesn't add to memory on object creation but only requests it on calling it? 那么这是否使用了较少的内存,因为它不会在对象创建时添加到内存中,而只会在调用时请求它?

Aaand if none of the above is any good... I guess I could put them into a seperate view file so then I can use HTML and do anything fancy as well. Aaand如果以上都不是一件好事...我想我可以将它们放到单独的视图文件中,这样我就可以使用HTML并做任何花哨的事情了。 I guess this is the ultimate solution but I'm looking more into how PHP handles memory... anywhere I can find any information? 我想这是最终的解决方案,但是我正在研究PHP如何处理内存...在任何可以找到任何信息的地方?

Thanks, Dom 谢谢,唐

Is the description the same between objects of the same class? 同一类别的对象之间的描述是否相同? use static attribute for common values between objects of the same class. 对相同类的对象之间的公共值使用静态属性。

I cannot say for sure, but I don't think there is a difference in what you proposed, because the string will have to exist in memory because it exists in the code; 我不能肯定地说,但我认为您提出的建议没有区别,因为字符串必须存在于内存中,因为它存在于代码中。 it just won't be assigned to a variable until the function is called. 在调用该函数之前,它不会分配给变量。 (If, however, you call the same function 100 times for 100 different variables, then it will be adding space in memory, and you may be able to save some of that space via a function call; but beware this may take more processing time). (但是,如果你调用同一个函数为100个不同的变量100次,那么这是在内存中添加空间,你可以节省一些通过函数调用空间;但要注意,这可能需要更多的处理时间)。

You may be interested in the some documentation on memory management in PHP 您可能对PHP中有关内存管理一些文档感兴趣

Here is an interesting book 这是一本有趣的书

A couple quick thoughts: 几个简单的想法:

  • Performance is more than RAM; 性能胜过RAM。 it's processing time. 这是处理时间。 And simply put, making a function call adds processing time (if you have any Assembly Language training, you know it takes a few processor cycles). 简而言之,进行函数调用会增加处理时间(如果您接受过汇编语言培训,那么您会花费几个处理器周期)。 This is relatively moot for modern programmers, but C functions like malloc take a really long time to run, so making fewer calls to allocate memory can be better. 对于现代程序员而言,这相对来说没有什么意义,但是像malloc这样的C函数运行起来会花费很长的时间,因此减少分配内存的调用可能会更好。
  • Know where your data will be coming from and going (if it's static data, try the static keyword; if it changes rapidly, look into databases like MySQL, or how to parse the data in XML or JSON. 知道数据将在何处来回(如果是静态数据,请尝试使用static关键字;如果数据变化迅速,请查看MySQL之类的数据库,或者如何解析XML或JSON中的数据。

These are two ways to avoid having thousands of descriptions in your PHP memory at the same time: 这是避免在PHP内存中同时包含数千个描述的两种方法:

If there are a (limited) number of different stereotypes sharing the same description, you can use integer constants to identify a stereotype, and create a function (typically static member) to translate the constant to the description when you need the description. 如果有(有限的)多个相同的构造型共享相同的描述,则可以使用整数常量来标识构造型,并在需要描述时使用函数(通常为静态成员)将常数转换为描述。

If each object has a different description, but is taken from some external data source (ie not PHP memory) you can also consider retrieving the description from that data source at the moment you really need it. 如果每个对象都有不同的描述,但取自某个外部数据源(即不是PHP内存),则您也可以在真正需要时考虑从该数据源检索描述。

Additionally, if you have to load the same descriptions in memory on each page request, you can also consider using some type of cache, such as Memcache or Redis and retrieve relevant descriptions when needed. 此外,如果必须在每个页面请求中在内存中加载相同的描述,则还可以考虑使用某种类型的缓存,例如MemcacheRedis,并在需要时检索相关的描述。

查看flyweight和代理模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM