![]() |
Which Provides Better Performance? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12) +--- Thread: Which Provides Better Performance? (/showthread.php?tid=77437) |
Which Provides Better Performance? - stupidscript - 09-01-2020 When loading models and connecting a database, is there a "best performance" way to do these things? 1) Loading Models: Code: use App\Models\MyModel; + $model=new MyModel(); + $model->myMethod($data); Code: $model = model('App\Models\MyModel'); + $model->myMethod($data); 2) Connecting to Database: Code: $db = \Config\Database::connect(); Code: $db = db_connect(); Code: $db = $this->db; RE: Which Provides Better Performance? - jreklund - 09-02-2020 These are the fastest ones, but the other ones add some extra functionality e.g. model() gives you a shared instance, so it's a matter on how you store yours, if you get the same performance. If you just make new ones all the time, that are slower again. Code: use App\Models\MyModel; + $model=new MyModel(); + $model->myMethod($data); Code: $db = $this->db; RE: Which Provides Better Performance? - stupidscript - 09-02-2020 Thank you for your reply. It makes sense to me, except the "shared instance" note. My understanding is that a "shared instance" is always available to all processes, once it is instantiated, and maintains updates made to it by every process that uses it. (loosely ![]() Wouldn't accessing a "shared instance" be less of a resource load than creating a new instance? Thanks, again. RE: Which Provides Better Performance? - jreklund - 09-03-2020 It's, it depends on how many times you use it yourself. If you just pass it along all the time it will be faster than model(). But if you load it separately in all files. It would be slower. RE: Which Provides Better Performance? - stupidscript - 09-03-2020 Okay, I think I got it, now. I appreciate your help. |