为什么 switch(true) 的 NPath 复杂度比 if() elseif() 要小?

2022-08-31 01:18:14

我有这个函数,负责将文件名和哑剧类型转换为更“人性化”的东西(例如file.png,image/ png到[Image,PNG])。我发现有趣的是,语句组的 NPath 复杂性高于语句。if() elseif()switch(true)

使用以下代码,PHP Mess Detector 输出 NPath 4410:

public function humanKind()
{
    $typeRA = explode("/", strtolower($this->type));
    $fileRA = explode(".", $this->name);
    $fileType = strtoupper($fileRA[count($fileRA) - 1]);

    switch($typeRA[0]) {
        case "image":
            $humanType = "Image";
            break;
        case "video":
            $humanType = "Video";
            break;
        case "audio":
            $humanType = "Sound";
            break;
        case "font":
            $humanType = "Font";
            break;
        default:
            $humanType = "File";
    }

    switch ($this->type) {
        case "application/msword":
        case "application/pdf":
        case "applicaiton/wordperfect":
        case "text/plain":
        case "text/rtf":
        case "image/vnd.photoshop":
        case "image/psd":
        case "image/vnd.adobe.photoshop":
        case "image/x-photoshop":
        case "application/xml":
        case "application/x-mspublisher":
        case "text/html":
        case "application/xhtml+xml":
        case "text/richtext":
        case "application/rtf":
        case "application/x-iwork-pages-sffpages":
        case "application/vnd.apple.pages":
            $humanType = "Document";
            break;
        case "application/vnd.ms-excel":
        case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
        case "application/x-iwork-numbers-sffnumbers":
        case "application/vnd.apple.numbers":
            $humanType = "Spreadsheet";
            break;
        case "application/vnd.ms-powerpoint":
        case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
        case "application/vnd.openxmlformats-officedocument.presentationml.slideshow":
        case "application/x-iwork-keynote-sffkey":
        case "application/vnd.apple.keynote":
            $humanType = "Slideshow";
            break;
        case "application/zip":
        case "application/x-zip-compressed":
        case "application/x-compressed":
        case "application/x-compress":
        case "application/x-rar-compressed":
        case "applicaiton/x-7z-compressed":
        case "application/x-ace-compressed":
            $humanType = "Archive";
            break;
        case "text/x-vcard":
        case "text/x-ms-contact":
            $humanType = "Contact";
            break;
        case "text/x-php":
        case "application/x-dosexec":
        case "application/x-xpinstall":
        case "application/x-opera-extension":
        case "application/x-chrome-extension":
        case "application/x-perl":
        case "application/x-shockwave-flash":
        case "application/java-archive":
            $humanType = "Program";
            break;
        case "application/vnd.ms-fontobject":
        case "application/font-woff":
        case "application/x-font-truetype":
        case "application/x-font-opentype":
        case "application/x-font-ttf":
        case "application/font-sfnt":
            $humanType = "Font";
            break;
    }

    // Special Cases
    if ($humanType == "Archive" && $fileType == "APK") { // Android App
        $humanType = "App";
    } elseif ($humanType == "Archive" && $fileType == "XPS") {
        $humanType = "Document";
    } elseif ($this->type == "application/xml" && $fileType == "CONTACT") {
        $humanType = "Contact";
    } elseif ($this->type == "application/octet-stream" && $fileType == "JNT") {
        $humanType = "Document";
    }

    if (strlen($fileType) > 4) {
        $fileType = "";
    }

    return array($humanType, $fileType);

如果我们用以下方式替换特殊情况:if elseif

    // Special Cases
    switch(true) {
        case ($humanType == "Archive" && $fileType == "APK"): // Android App
            $humanType = "App";
            break;
        case ($humanType == "Archive" && $fileType == "XPS"):
            $humanType = "Document";
            break;
        case ($this->type == "application/xml" && $fileType == "CONTACT"):
            $humanType = "Contact";
            break;
        case ($this->type == "application/octet-stream" && $fileType == "JNT"):
            $humanType = "Document";
            break;
    }

PHP Mess Detector 报告了 1960 年的 NPath 复杂性。

这是为什么呢?是什么让 switch(true) 比在我看来几乎相同的控制结构更不复杂?


答案 1

由于 NPath 复杂性测量的是完全覆盖代码所需的单元测试数量,因此 2 个“特殊情况”实现之间应该没有区别。

但是在计算上存在一些差异。让我们逐步了解 2 个“特殊情况”实现,并手动计算 NPath 复杂性:

NPath 复杂性if .. elseif ..

if ($humanType == "Archive" && $fileType == "APK") { // Android App
    $humanType = "App";
} 
elseif ($humanType == "Archive" && $fileType == "XPS") {
    $humanType = "Document";
} 
elseif ($this->type == "application/xml" && $fileType == "CONTACT") {
    $humanType = "Contact";
} 
elseif ($this->type == "application/octet-stream" && $fileType == "JNT") {
    $humanType = "Document";
}

此语句导致 NPath 复杂度为 9:1 点表示,每个运算符 1 点,每个运算符 1 点。(1 + 4 + 4 = 9)if .. elseif(expr)&&

NPath 复杂性switch(true)

switch(true) {
    case ($humanType == "Archive" && $fileType == "APK"): // Android App
        $humanType = "App";
        break;
    case ($humanType == "Archive" && $fileType == "XPS"):
        $humanType = "Document";
        break;
    case ($this->type == "application/xml" && $fileType == "CONTACT"):
        $humanType = "Contact";
        break;
    case ($this->type == "application/octet-stream" && $fileType == "JNT"):
        $humanType = "Document";
        break;
}

此语句导致 NPath 复杂度仅为 4:0 点,因为它不包含 or 运算符,每个标签包含 1 点。(0 + 4 = 4)switch(true)&&||case

NPath 函数的复杂性humanKind

为每个语句计算 NPath 值,然后将这些值相乘。没有“特殊情况”语句的函数的 NPath 复杂度为 490。乘以语句 9 的 NPath 值,得到 NPath 复杂度 4410。乘以语句 4 的 NPath 值,您得到的复杂度仅为 1960。就这样!if .. else if ..switch(true)

现在我们知道了:NPath 复杂性不测量 switch 语句中大小写标签的表达式复杂性!


答案 2

通常,switch 可能比 if/elseif 更快,因为 switch 语句评估条件一次,然后与每种情况进行比较。

我的理解是,switch语句中的案例是内部索引的,因此您可能会因此获得更好的性能(尽管我找不到讨论此内容的原始文章,因此我无法证明这一点)。

我还会想象 switch 语句的 AST 比等效的 if/elseif 语句简单得多。

编辑:

在基于 C 的语言(以及最有可能的其他语言)中,当 switch 语句的长度超过 4-5 个事例时,它们会作为列表/哈希表实现。这意味着每个项目的访问时间变得相同。而在 if/elseif 块中,没有这样的优化。

编译器更容易处理这些类型的 switch 语句,因为它可以对不同的条件做出更多假设。因此,复杂性较低。任意情况的查找为 O(1)。这再次链接到我之前的陈述,即交换机的AST很可能要简单得多。

编辑#2:

在更多的CS术语中,编译器可以使用分支表(或跳转)来减少switch语句的CPU时间:http://en.wikipedia.org/wiki/Branch_table


推荐