CodeIgniter NOW() insert: what's the fastest w

2019-09-02 17:23发布

问题:

What code will be faster (or better):

$this->db->set('date', 'NOW()', FALSE);

or

$data['date'] = date('Y-m-d H:i:s');

回答1:

In general all function calls will be slower because of the overhead. However I won't say function calls in PHP are very expensive as qwertzman answered. Ok it may be slower compared to other languages, but that really isn't the point. Have a look at this comparison with 1,000,000 function calls. You see what you gain?

When talking about which piece of code is faster (in your OP) you're talking about micro-optimization and is really something you shouldn't have to worry about.

The real question is which piece of code is: better maintainable, readable, understandable.



回答2:

In general calling a function is very expensive in PHP. So what is faster, the date function or the set function? It is the date function. Just look at what the set function is doing on line 907 of DB_active_rec.php, it's doing all kind of stuff that doesn't seem to be necessary.

So which is better? Go for the faster option I guess. The set() function might be overkill here (although I always use set(), out of habit/lazyness I guess).