-6

What is more efficient for a PHP variable, $timelimit = 10*60; //10 minutes or $timelimit = 600; //10 minutes?

I feel as though $timelimit = 600; //10 minutes would be more efficient/optimized since I am telling PHP that the $timelimit is 600, and I am not asking it to do any calculations.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
PRAWKC
  • 7
  • 2
  • 3
    doing something, is always going to take longer than doing nothing. but this is terrible Micro-Optimization –  Sep 03 '15 at 23:45
  • 1
    [Premature optimization is the root of all evil](http://c2.com/cgi/wiki?PrematureOptimization) – Barmar Sep 04 '15 at 00:23
  • When you bring us a question that has suppositions, your suppositions should be reflected by data. Show us your benchmark, your inputs, what your outputs were. Then ask us. Don't say "This is marginally faster than that, should I use it?" without showing us the benchmarks. – George Stocker Sep 04 '15 at 00:57

1 Answers1

2

Efficient to what?

If readability, 60 * 10 is much more understandable than 600 in terms of timing. For performance, 600 is probably a tiny bit better.

Let's say I want to say one day, I'd write 60 * 60 * 24. It seems cleanier than 86400

Sometimes, efficiency is the way other people (or you, later) can read your code and do something with it.

# cat test2.php
<?php
$time=microtime();
for($i=0; $i<1000000;$i++){
  $t = 60*60*24;
}
echo microtime() - $time . "\n";

$time=microtime();
for($i=0; $i<1000000;$i++){
  $t = 86400;
}
echo microtime() - $time . "\n";


# php test2.php
0.05881
0.042382

Over 1 000 000 tries, you lose 0.016 seconds... This isn't nothing, but it's quite close to nothing. I'd prefer to have a clean code.

Loïc
  • 11,804
  • 1
  • 31
  • 49
  • Performance wise efficiency. I do agree that 60 * 60 * 24 is more clean however how many times would you need to calculate that? Once maybe twice if you decide to change the code in the future. Where as the code has to keep calculating this number each and every time its run. Do you know what I mean or what I am trying to say? – PRAWKC Sep 04 '15 at 00:07