Time Since Last Post

If you think something is wrong with a tutorial, or something could be done better, discuss it here.

Time Since Last Post

Postby wide_load » Thu Feb 11, 2010 2:04 am

http://www.youtube.com/watch?v=Ph2s7bCN23Q

I think
  1. switch(true){
  2.     case ($this === $that):
  3.         // stuff
  4.     break;
  5. }


is unnecessary use of a switch, I don't think its really what they are for.

basically it would work just as well if you remove the switch and replace case with if and else if

switch only makes sense to me when you use some kind of condition in the switch($something) statement...

EDIT: "im not going to wait aroud a year to test it" ... you could have edited the timestamps in the table...
Image
User avatar
wide_load
Top Contributor
 
Posts: 5616
Joined: Thu Aug 13, 2009 1:04 pm
Online: 13d 21h 56m 33s
Karma: 45

Re: Time Since Last Post

Advertisment

Advertisment
 

Re: Time Since Last Post

Postby alex » Mon Feb 22, 2010 8:03 pm

I was demonstrating the non-conditional use of switch statements. I'm trying to demonstrate moving away from IF ELSE. Switch statements are also a faster alternative to IF ELSE.
User avatar
alex
Founder
 
Posts: 483
Joined: Mon Apr 20, 2009 9:55 pm
Location: London, UK.
Online: 4d 11h 32m 26s
Karma: 11

Re: Time Since Last Post

Postby wide_load » Mon Feb 22, 2010 8:26 pm

alex wrote:I was demonstrating the non-conditional use of switch statements. I'm trying to demonstrate moving away from IF ELSE. Switch statements are also a faster alternative to IF ELSE.


are you sure about that ?

surely both methods perform the same logic ? :?
Image
User avatar
wide_load
Top Contributor
 
Posts: 5616
Joined: Thu Aug 13, 2009 1:04 pm
Online: 13d 21h 56m 33s
Karma: 45

Re: Time Since Last Post

Postby alex » Fri Apr 02, 2010 12:06 pm

wide_load wrote:are you sure about that ?

surely both methods perform the same logic ? :?


They are a faster alternative, yes.
User avatar
alex
Founder
 
Posts: 483
Joined: Mon Apr 20, 2009 9:55 pm
Location: London, UK.
Online: 4d 11h 32m 26s
Karma: 11

Re: Time Since Last Post

Postby wide_load » Mon May 10, 2010 4:09 pm

alex wrote:
wide_load wrote:are you sure about that ?

surely both methods perform the same logic ? :?


They are a faster alternative, yes.


test script
  1. <?php
  2. header('Content-type: text/plain');
  3.  
  4. $start = microtime(true);
  5.  
  6. for ($i = 0; $i < 10000000; $i++){
  7.     if ('one' == true){
  8.         $var = true;
  9.     }else if ('this' == 'that'){
  10.         $var = true;
  11.     }else if (1 == 4){
  12.         $var = true;
  13.     }else{
  14.         $var = false;
  15.     }
  16. }
  17.  
  18. echo 'If:     '.(microtime(true) - $start);
  19. $start = microtime(true);
  20.  
  21. for ($i = 0; $i < 10000000; $i++){
  22.     switch(true){
  23.         case ('one' == true):
  24.             $var = true;
  25.         break;
  26.        
  27.         case ('this' == 'that'):
  28.             $var = true;
  29.         break;
  30.        
  31.         case (1 == 4):
  32.             $var = true;
  33.         break;
  34.        
  35.         default:
  36.             $var = false;
  37.         break;
  38.     }
  39. }
  40.  
  41. echo "\nSwitch: ".(microtime(true) - $start);
  42.  
  43. ?>


on my server this output
  1. If:     1.699187040329
  2. Switch: 2.085853099823


I know its not a very real world test, but it does suggest that switches are not always faster than if else if else ...

comparing the condition next to the case i.e. case (1 == 2) in a switch(true) block is the same as
  1. if ((1 == 2) === true){

which I think may be the reason for the slowness, its doing an extra comparison, the numbers back this up the switch is about 22% slower.

when used the way it is intended the speed difference is much smaller, but still the switch is slower.
test script
  1. <?php
  2. header('Content-type: text/plain');
  3.  
  4. $start = microtime(true);
  5.  
  6. for ($i = 0; $i < 10000000; $i++){
  7.     if ('one' == true){
  8.         $var = true;
  9.     }else if ('this' == true){
  10.         $var = true;
  11.     }else if (1 == true){
  12.         $var = true;
  13.     }else{
  14.         $var = false;
  15.     }
  16. }
  17.  
  18. echo 'If:     '.(microtime(true) - $start);
  19. $start = microtime(true);
  20.  
  21. for ($i = 0; $i < 10000000; $i++){
  22.     switch(true){
  23.         case 'one':
  24.             $var = true;
  25.         break;
  26.        
  27.         case 'this':
  28.             $var = true;
  29.         break;
  30.        
  31.         case 1:
  32.             $var = true;
  33.         break;
  34.        
  35.         default:
  36.             $var = false;
  37.         break;
  38.     }
  39. }
  40.  
  41. echo "\nSwitch: ".(microtime(true) - $start);
  42.  
  43. ?>


on the same server as before this output
  1. If:     1.6776959896088
  2. Switch: 1.9025719165802


again, i know this is not a real world example, but it does show that the switch is slower, and must be doing more comparison when used in the way (which i believe to be incorrect) you do in the video.

hopefully you will be able to explain this.
Image
User avatar
wide_load
Top Contributor
 
Posts: 5616
Joined: Thu Aug 13, 2009 1:04 pm
Online: 13d 21h 56m 33s
Karma: 45

Re: Time Since Last Post

Postby alex » Mon May 17, 2010 10:15 am

Try the two statements but in two different files.
User avatar
alex
Founder
 
Posts: 483
Joined: Mon Apr 20, 2009 9:55 pm
Location: London, UK.
Online: 4d 11h 32m 26s
Karma: 11

Re: Time Since Last Post

Postby wide_load » Mon May 17, 2010 6:40 pm

alex wrote:Try the two statements but in two different files.


that made no difference at all.

I don't see why it would do :?
Image
User avatar
wide_load
Top Contributor
 
Posts: 5616
Joined: Thu Aug 13, 2009 1:04 pm
Online: 13d 21h 56m 33s
Karma: 45


Return to Tutorial Feedback

Who's online?

Users browsing this forum: No registered users and 1 guest