php字符串转驼峰性能对比

使用apache ab工具进行测试了下。

ab -n100 -c10 https://you domain/

方法1:原生php函数


$str = 'my_name_is_bill';
echo implode('', array_map(function($val) {
    return ucfirst($val);
}, explode('_', $str)));

测试结果

Concurrency Level:      10
Time taken for tests:   0.657 seconds
Complete requests:      100
Failed requests:        0
Total transferred:      16700 bytes
HTML transferred:       1200 bytes
Requests per second:    152.28 [#/sec] (mean)
Time per request:       65.669 [ms] (mean)
Time per request:       6.567 [ms] (mean, across all concurrent requests)
Transfer rate:          24.83 [Kbytes/sec] received

方法2: 自己编写php代码

$str = 'my';

if ( strpos($str, '_') === false ) {
    echo ucfirst($str);exit;
}

$return = '';
foreach ( explode('_', $str) as $val ) {
    $return .= ucfirst($val);
}

echo $return;

测试结果

Concurrency Level:      10
Time taken for tests:   0.632 seconds
Complete requests:      100
Failed requests:        0
Total transferred:      15700 bytes
HTML transferred:       200 bytes
Requests per second:    158.24 [#/sec] (mean)
Time per request:       63.195 [ms] (mean)
Time per request:       6.320 [ms] (mean, across all concurrent requests)
Transfer rate:          24.26 [Kbytes/sec] received
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,830评论 25 709
  • Welcome 目前网络上充斥着大量的陈旧信息,让PHP新手误入歧途,传播着错误的实践和糟糕的代码,这必须得到纠正...
    layjoy阅读 21,749评论 7 118
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,067评论 19 139
  • 基础知识 iOS提供了两种管理内存的方式,分别为MRC(手动)和ARC(自动)。 MRC(人工引用计数),手动管理...
    是小胖阿阅读 277评论 0 0
  • 越来越多的人希望自己活成红楼梦里宝姐姐的样子。薛宝钗确实是一个难得的姑娘。上能赢得贾府当家人贾母和未来当家人王夫人...
    三人共读阅读 1,147评论 7 5