下面的代碼可確保當用戶訪問控制面板時,他們會通過快速驗證過程來驗證其實體是什麼。例如,如果用戶是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.0000001sec時,情況並非如此。每個開發人員都應該編寫易於閱讀的代碼,因此 - 易於優化(但不像您所做的那樣)。 – zerkms 2011-02-08 02:27:46
我認爲這看起來相當可接受,對於電腦來說它肯定不是*效率低下的。你也許可以隨便改變一些代碼,但似乎並不能從根本上改變很多。 – deceze 2011-02-08 02:27:49
你應該停止使用幻數並定義一些描述權限級別的常量。例如`PERMISSION_VIDEO_FEED「。 – deceze 2011-02-08 02:31:28