代码之家  ›  专栏  ›  技术社区  ›  SoapBox

快速案件计数

  •  1
  • SoapBox  · 技术社区  · 17 年前

    ucwords(strtolower($str))

    检查这个最快的方法是什么?似乎 for

    以下是我所拥有的,我认为它会太慢:

    function fixCase($str)
    {
        $uc = 0;
        $lc = 0;
        for($i=0;$i<strlen($str);$i++)
        {
            if ($str[$i] >= 'a' && $str[$i] <= 'z')
                $lc++;
            else if ($str[$i] >= 'A' && $str[$i] <= 'Z')
                $uc++;
        }
    
        if ($uc == 0 || $lc == 0)
        {
            return ucwords(strtolower($str));
        }
    }
    
    7 回复  |  直到 17 年前
        1
  •  6
  •   J.C. Inacio    17 年前

    只需使用字符串比较(区分大小写)

    function fixCase($str)
    {
      if ( 
           (strcmp($str, strtolower($str)) === 0) || 
           (strcmp($str, strtoupper($str)) === 0) ) 
      {
        $str = ucwords(strtolower($str));
      }
    
      return $str;
    }
    
        2
  •  1
  •   Chad Birch    17 年前

    就我个人而言,我会用这种算法遍历字符串的字符:

    • 如果 任何 character与第一个字符的情况不同,请断开循环并返回字符串。

    编辑:

    // returns 0 if non-alphabetic char, 1 if uppercase, 2 if lowercase
    function getCharType($char)
    {
        if ($char >= 'A' && $char <= 'Z')
        {
            return 1;
        }
        else if ($char >= 'a' && $char <= 'z')
        {
            return 2;
        }
        else
        {
            return 0;
        }
    }
    
    function fixCase($str)
    {
        for ($i = 0; $i < strlen($str); $i++)
        {
            $charType = getCharType($str[$i]);
            if ($charType != 0)
            {
                $firstCharType = $charType;
                break;
            }
        }
    
        for ($i = $i + 1; $i < strlen($str); $i++)
        {
            $charType = getCharType($str[$i]);
            if ($charType != $firstCharType && $charType != 0)
            {
                return $str;
            }
        }
    
        if ($firstCharType == 1) // uppercase, need to convert to lower first
        {
            return ucwords(strtolower($str));
        }
        else if ($firstCharType == 2) // lowercase, can just ucwords() it
        {
            return ucwords($str);
        }
        else // there were no letters at all in the string, just return it
        {
            return $str;
        }
    }
    
        3
  •  1
  •   Community Mohan Dere    9 年前

    你可以试试我发布的字符串用例测试函数 here

    function getStringCase($subject)
    {
        if (!empty($subject))
        {
            if (preg_match('/^[^A-Za-z]+$/', $subject))
                return 0;   // no alphabetic characters
            else if (preg_match('/^[^A-Z]+$/', $subject))
                return 1;   // lowercase
            else if (preg_match('/^[^a-z]+$/', $subject))
                return 2;   // uppercase
            else
                return 3;   // mixed-case
        }
        else
        {
            return 0;   // empty
        }
    }
    
        4
  •  1
  •   Mark    17 年前

    如果你想避免修复已经混合大小写的字符串是为了提高效率,那么你可能会浪费时间,转换每个字符串,无论其当前状态如何:

    function fixCase($str)
    {
        return ucwords(strtolower($str));
    }
    

    如果它的运行速度比字符串的公认答案慢,我会非常惊讶,字符串的长度通常是你想要命名的大小写,这是你需要担心的一个条件。

    然而,如果有充分的理由避免转换已经混合大小写的字符串,例如你想在大小写中保留一些预期的含义,那么是的,jcinacio的答案肯定是最简单、最有效的。

        5
  •  0
  •   Indeed is Trash    17 年前

    检查字符串=小写(string)还是字符串=大写(string)不是更容易吗?如果是这样,就留下它。否则执行您的操作。

        6
  •  0
  •   SoapBox    17 年前

    代码:

    function method1($str)
    {
        if (strcmp($str, strtolower($str)) == 0)
        {
            return ucwords($str);
        }
        else if (strcmp($str, strtoupper($str)) == 0)
        {
            return ucwords(strtolower($str));
        }
        else
        {
            return $str;
        }
    }
    
    // returns 0 if non-alphabetic char, 1 if uppercase, 2 if lowercase
    function getCharType($char)
    {
        if ($char >= 'A' && $char <= 'Z')
        {
            return 1;
        }
        else if ($char >= 'a' && $char <= 'z')
        {
            return 2;
        }
        else
        {
            return 0;
        }
    }
    
    function method2($str)
    {
        for ($i = 0; $i < strlen($str); $i++)
        {
            $charType = getCharType($str[$i]);
            if ($charType != 0)
            {
                $firstCharType = $charType;
                break;
            }
        }
    
        for ($i = $i + 1; $i < strlen($str); $i++)
        {
            $charType = getCharType($str[$i]);
            if ($charType != $firstCharType && $charType != 0)
            {
                return $str;
            }
        }
    
        if ($firstCharType == 1) // uppercase, need to convert to lower first
        {
            return ucwords(strtolower($str));
        }
        else if ($firstCharType == 2) // lowercase, can just ucwords() it
        {
            return ucwords($str);
        }
        else // there were no letters at all in the string, just return it
        {
            return $str;
        }
    }
    
    function method0($str)
    {
            $uc = 0;
            $lc = 0;
            for($i=0;$i<strlen($str);$i++)
            {
                    if ($str[$i] >= 'a' && $str[$i] <= 'z')
                            $lc++;
                    else if ($str[$i] >= 'A' && $str[$i] <= 'Z')
                            $uc++;
            }
    
            if ($uc == 0 || $lc == 0)
            {
                    return ucwords(strtolower($str));
            }
    }
    
    
    function test($func,$s)
    {
        $start = gettimeofday(true);
        for($i = 0; $i < 1000000; $i++)
        {
            $s4 = $func($s);
        }
        $end = gettimeofday(true);
        echo "$func Time: " . ($end-$start) . " - Avg: ".sprintf("%.09f",(($end-$start)/1000000))."\n";
    }
    
    
    $s1 = "first String";
    $s2 = "second string";
    $s3 = "THIRD STRING";
    
    test("method0",$s1);
    test("method0",$s2);
    test("method0",$s3);
    
    test("method1",$s1);
    test("method1",$s2);
    test("method1",$s3);
    
    test("method2",$s1);
    test("method2",$s2);
    test("method2",$s3);
    

    结果:

    method0 Time: 19.2899270058 - Avg: 0.000019290
    method0 Time: 20.8679389954 - Avg: 0.000020868
    method0 Time: 24.8917310238 - Avg: 0.00002489   
    method1 Time: 3.07466816902 - Avg: 0.000003075
    method1 Time: 2.52559089661 - Avg: 0.000002526
    method1 Time: 4.06261897087 - Avg: 0.000004063
    method2 Time: 19.2718701363 - Avg: 0.000019272
    method2 Time: 35.2485661507 - Avg: 0.000035249
    method2 Time: 29.3357679844 - Avg: 0.000029336
    
        7
  •  0
  •   Michael Borgwardt    17 年前

    推荐文章