Welcome Guest, Not a member yet? Register   Sign In
Which Provides Better Performance?
#1

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);
  = or =
Code:
$model = model('App\Models\MyModel'); + $model->myMethod($data);

2) Connecting to Database:

Code:
$db = \Config\Database::connect();
  = or =
Code:
$db = db_connect();
  = or =
Code:
$db = $this->db;
Reply
#2

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;
Reply
#3

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 Smile )

Wouldn't accessing a "shared instance" be less of a resource load than creating a new instance?

Thanks, again.
Reply
#4

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.
Reply
#5

Okay, I think I got it, now. I appreciate your help.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB