欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > php >内容正文

php

strcompare php,PHP中的startswith()和endsWith()函数

发布时间:2023/12/3 php 50 豆豆
生活随笔 收集整理的这篇文章主要介绍了 strcompare php,PHP中的startswith()和endsWith()函数 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

泛舟湖上清波郎朗

功能function substr_startswith($haystack, $needle) {

    return substr($haystack, 0, strlen($needle)) === $needle;}function preg_match_startswith($haystack, $needle) {

    return preg_match('~' . preg_quote($needle, '~') . '~A', $haystack) > 0;}function substr_compare_startswith($haystack, $needle) {

    return substr_compare($haystack, $needle, 0, strlen($needle)) === 0;}function strpos_startswith($haystack, $needle) {

    return strpos($haystack, $needle) === 0;}function strncmp_startswith($haystack, $needle) {

    return strncmp($haystack, $needle, strlen($needle)) === 0;}function strncmp_startswith2($haystack, $needle) {

    return $haystack[0] === $needle[0]

        ? strncmp($haystack, $needle, strlen($needle)) === 0

        : false;}试验echo 'generating tests';for($i = 0; $i 

    if($i % 2500 === 0) echo '.';

    $test_cases[] = [

        random_bytes(random_int(1, 7000)),

        random_bytes(random_int(1, 3000)),

    ];}echo "done!\n";$functions = ['substr_startswith', 'preg_match_startswith', 'substr_compare_startswith', 'strpos_startswith', 

    'strncmp_startswith', 'strncmp_startswith2'];$results = [];foreach($functions as $func) {

    $start = microtime(true);

    foreach($test_cases as $tc) {

        $func(...$tc);

    }

    $results[$func] = (microtime(true) - $start) * 1000;}asort($results);foreach($results as $func => $time) {

    echo "$func: " . number_format($time, 1) . " ms\n";}结果(PHP 7.0.9)(排序最快至最慢)strncmp_startswith2: 40.2 ms

strncmp_startswith: 42.9 ms

substr_compare_startswith: 44.5 ms

substr_startswith: 48.4 ms

strpos_startswith: 138.7 ms

preg_match_startswith: 13,152.4 ms结果(PHP 5.3.29)(排序最快至最慢)strncmp_startswith2: 477.9 ms

strpos_startswith: 522.1 ms

strncmp_startswith: 617.1 ms

substr_compare_startswith: 706.7 ms

substr_startswith: 756.8 ms

preg_match_startswith: 10,200.0 msstartsWith_bench mark.php

总结

以上是生活随笔为你收集整理的strcompare php,PHP中的startswith()和endsWith()函数的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。