douhu7807 2011-03-23 20:18
浏览 20
已采纳

我应该多久创建一个类的新实例?

I'm not a "die hard" coder and I need some advice.

I'm developing a website where users may search for a store or a brand.

I've created a class called Search and Store.

There are two ways search is executed: "jQuery Live Search" and "normal search".

Live search is triggered for each character entered above 2 characters. So if you enter 5 characters, a search is performed 3 times. If the store you are looking for is in the dropdown list, you can click the store and the store page will be loaded.

The other search is when you click the search button after entering 3 or more characters.

Every time a search is performed, the following code is executed

$search = new Search();
$result = $search->search($_GET);

Each time a store page is loaded a $store = new Store() is executed.

My question is this:

Let's assume I get a very successful website and I have aroun 100 users per hour. Each user searches at least 3 times and looks at least 5 stores.

That means between 300 and 900 search objects are created every hour and 500 store objects.

Is it bad or good to create so many new objects?

I've read a bit about Singleton, but many advices against this.

How should I do this to achieve best performance? Any specific design pattern I should use?

  • 写回答

3条回答 默认 最新

  • douchan6512 2011-03-23 20:26
    关注

    I don't think that creating the classes will become a bottleneck for your site. Look at an MVC Framework like Zend Framework, and examine how many instances of classes are generated for every call. The overhead of creating an instance of a class is almost nothing, the search will put heat on your db(assuming you are using a db like mysql).

    I suggest using a timer for your jQuery Live search to do the search after the user stopped entering more characters. Like refreshing everytime the timer when a character has been entered and when the timer fires you can actually search.

    I think one of the bigger problems will be your database. If you have many reading requests a good caching layer like memcache may take a good heap of load from you DB.

    Optimizing your db for searches should be a good measure to hold performance high. There are many tweaks and best practices to follow to get the most out of the db you are using.

    As a comment of prodigitalson suggested diving into full text search with Lucene could even be more efficient than tuning the db.

    If Lucene is bit overhead for you, you may want to look at the Zend_Search_Lucene component, which does the same job and is written in php.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路