0

What code will be faster (or better):

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

or

$data['date'] = date('Y-m-d H:i:s');
Blueberry Hill
  • 119
  • 4
  • 12
  • What you're talking about here is a [tag:micro-optimization]. Honestly, worrying about this is completely pointless unless you can prove with a benchmark that your application performance is suffering in a meaningful way by using one approach or the other (Hint: Your application performance isn't suffering in any meaningful way if you use either approach) – GordonM Apr 02 '12 at 20:19
  • 3
    Any optimization you do to squeeze off another 0.001s is lost in the architectural and performance disaster known as 'CodeIgniter'. – tereško Apr 02 '12 at 20:53

2 Answers2

2

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.

Community
  • 1
  • 1
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
-1

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

qwertzman
  • 784
  • 1
  • 9
  • 23
  • 2
    Function calling in PHP really isn't very expensive. Either way, the asker is talking about [tag:micro-optimization] that (s)he shouldn't be worrying about. – GordonM Apr 02 '12 at 20:17
  • Before edownvoting you guys should seriously consider increasing your knowledge and background on PHP. http://stackoverflow.com/questions/3691625/why-are-php-function-calls-so-expensive – qwertzman Apr 02 '12 at 20:22
  • 3
    @qwertzman http://viper-7.com/xYVMjo. That's with 1,000,000 runs. Again: Define very expensive... I don't call that VERY expensive I call that some obvious overhead which shouldn't have any impact on OP. – PeeHaa Apr 02 '12 at 20:27
  • @RWPH: still approx. 2.5 times faster, no? – qwertzman Apr 02 '12 at 20:30
  • 1
    @qwertzman 0.16 seconds after a million functions calls is not expensive. It's certainly far less expensive than any DB or filesystem interaction that will be occurring during a script run. And FYI I've been doing PHP for over 10 years, so perhaps you should choose your words a bit more carefully. "Increase your knowledge and background" indeed. – GordonM Apr 02 '12 at 20:36
  • 2
    http://www.codinghorror.com/blog/2009/01/the-sad-tragedy-of-micro-optimization-theater.html – PeeHaa Apr 02 '12 at 20:39
  • @RWPH: I try to answer the question regardless of whether micro-optimization is good or bad, because it actually doesn't answer the question. – qwertzman Apr 02 '12 at 20:43
  • @Gordon: Function calls in PHP remain the most expensive of just about any mainstream language: http://shootout.alioth.debian.org/u32/benchmark.php?test=binarytrees&lang=all – qwertzman Apr 02 '12 at 20:49
  • 2
    @qwertzman And they still remain several orders of magnitude less expensive than interacting with the DB/Filesystem/Network sockets. The amount of work necessary in avoiding functional calls is completely disproportionate to the benefit you will see in improved app performance. If you're that worried about performance, then write your web app in assembler. Good luck with maintaining it. – GordonM Apr 02 '12 at 20:53
  • http://stackoverflow.com/questions/3470990/is-micro-optimization-worth-the-time/3471356#3471356 – qwertzman Apr 02 '12 at 20:53
  • 3
    @qwertzman , you just showed that you do not comprehend what was written in that comment – tereško Apr 02 '12 at 20:59