2011-02-08 106 views
0

下面的代碼可確保當用戶訪問控制面板時,他們會通過快速驗證過程來驗證其實體是什麼。例如,如果用戶是1級,他們只能訪問視頻源,這意味着他們沒有其他任何東西可用。PHP案例開關(效率)

當我查看代碼時,我可以看到調用情況1和3時的視頻饋送。我可能會喜歡另一種方法來提高代碼的效率。

我被告知一個可能的數組可能會讓事情變得更容易一些,但是這樣會更快。

switch ($_SESSION['permission']) { 
     case 1: // Level 1: Video Feed 
      include ("include/panels/videofeed.index.php"); 
      break; 
     case 2: // Level 2: Announcements/Courses/Teachers 
      include ("include/panels/announcements.index.php"); 
      include ("include/panels/courses.index.php"); 
      include ("include/panels/teachers.index.php"); 
      break; 
     case 3: // Level 3: Announcements/Video Feed/Courses/Teachers/Accounts/Logs 
      include ("include/panels/announcements.index.php"); 
      include ("include/panels/videofeed.index.php"); 
      include ("include/panels/courses.index.php"); 
      include ("include/panels/teachers.index.php"); 
      include ("include/panels/accounts.index.php"); 
      include ("include/panels/log.index.php");  
      break; 
     case 4: // Level 4: Teachers 
      include ("include/panels/teachers.index.php");   
    } 
+0

當您需要使混淆代碼贏得0.0000001sec時,情況並非如此。每個開發人員都應該編寫易於閱讀的代碼,因此 - 易於優化(但不像您所做的那樣)。 – zerkms 2011-02-08 02:27:46

+1

我認爲這看起來相當可接受,對於電腦來說它肯定不是*效率低下的。你也許可以隨便改變一些代碼,但似乎並不能從根本上改變很多。 – deceze 2011-02-08 02:27:49

+1

你應該停止使用幻數並定義一些描述權限級別的常量。例如`PERMISSION_VIDEO_FEED「。 – deceze 2011-02-08 02:31:28

回答

1

如果您使用require_once(如果可能的話),首先您可以運行更好。 第二點是縮短它的網址似乎每個都包含相同的。

也許嘗試在例如一個函數使用它:

錯誤?糾正我!

4

這是很好的方式。當你提到「重複」包括時,我認爲你不是指「效率」。你的意思是你可以通過使用切換器來壓縮你的代碼。

雖然這可能會使您的代碼更小,但它對效率(腳本運行的時間)沒有顯着影響,並且實際上會使代碼更難以閱讀。保持它。