Welcome Guest, Not a member yet? Register   Sign In
Nice Code Tipp is needed or how to minimize memory usage
#1

[eluser]Gewa[/eluser]
Hi, I am not a profi php programmer,so I liked the way codeigniter is developed. MVC is cool. But now when I am working with big datasets (yellowpages with 140.000 firms, and more than 800Categories) I need a tip how to minimize memory usage .

I have 2 general problems with project.

a) project is in UTF-8 and after cashing its showing unreadable characters ( when unicode cirilic or other languages texts are used)

b) memory usage of 3MB-6Mb...


Usually I call database directly from controller , then parsing data , generate HTML in controller, and pass it to views

lets say
Code:
$qq=$this->db->get('categories);


foreach($qq->result as $cat){


$data['mod_listing'].="<li><a href="/category/show_cat/">cat_id."/>".$cat->cat_name."</a>";
}

$this->load->view('cat_template',$data);

and then in cat_template.php just echoing $mod_listing....




After I discovered that if I generate the HTML inside the Model, and call this model directly from view file with making

Code:
&lt;? echo $this->cat_model->show_cats();?&gt;

will decrease memory usage lets say 10% not so much...



BUT WHAT IS THE PERFECT WAY?


HOW YOU DO?
#2

[eluser]TheFuzzy0ne[/eluser]
How does the data in the database view through PHPMyAdmin? I'm guessing that you haven't set the character coding in the output HTML.

As for memory usage, I've no idea which part of your code is using up so much memory, so I can't help you there.
#3

[eluser]jedd[/eluser]
Quote:b) memory usage of 3MB-6Mb...

Memory usage will be Fun. Have you looked at the [url="http://ellislab.com/codeigniter/user-guide/general/profiling.html"]Profiling Class[/url]? That'll give you some amount of insight, but possibly not detailed enough for your purposes.

In general terms, you can look at destroying objects and data once you've finished with them (rather than waiting for PHP to clean them up on exit). You can minimise the amount of data you pull from databases (most people seem to get more than they need, and let PHP cherry pick what they actually require). You can look at a separate database - not OLAP but in the same spirit, especially if this data can be 24 hours stale, for example, you may be able to dump a smaller database that contains only the data you want and/or in a more efficient format for what you're doing.

You are likely to do lots of comparisons of different approaches to see which is faster for your particular data and algorithms.


Quote:Usually I call database directly from controller , then parsing data , generate HTML in controller, and pass it to views

Staring into my crystal ball, I can see problems in your future.

If your database calls are in your controller, then every time you come up with an optimisation for a section of code, you have to replicate that to all your controllers that happen to use that (or similar) code. This could, by the sounds of it, be multiple methods within multiple controllers.

So, to come back to your original question, you could put database calls into models - it'll make it faster to make it faster, if you see what I mean.

Generating HTML in controllers will suffer the exact same type of problem, of course, though probably not to the same degree.

Or you can just buy a faster computer.
#4

[eluser]dmitrybelyakov[/eluser]
Hi,

As earlier mentioned you should structure your code properly, especially if you think mvc is cool.
Then in your Models, when you get the data or in Controllers where you work with it to generate Views - implement cache. Use it for large data requests.

And for the encoding problem - set your config.php, database.php & database itself to UTF-8 this should resolve your problem with encoding.

Good luck.




Theme © iAndrew 2016 - Forum software by © MyBB