---
title: "分割字符串"
author: "jedy"
date: "2005-12-01T07:47:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2005-12-01T07:47:00Z"
id: 304773
url: "https://jedy.iteye.com/blog/304773"
---

```

#!/usr/bin/perl -w

###########################################
#
#  将一个字符串分成3个3个一组的6种方法
#  执行的速度依次增加
#
###########################################

use Benchmark qw(cmpthese);

cmpthese 100, {
    'Name1' => '$a="123456789" x 1000;($b=$a)=~s/(.{3})/$1|/g;$c=join ",",split /\|/,$b;',
    'Name2' => '$a="123456789" x 1000;@i=0..int((length($a)-1)/3);map {$_*=3} @i;$c=join ",",map {substr($a,$_,3)} @i;',    
    'Name3' => '$a="123456789" x 1000;@i=map {$_*3} 0..int((length($a)-1)/3);$c=join ",",map {substr($a,$_,3)} @i;',    
    'Name4' => '$a="123456789" x 1000;$c=join ",",map {substr($a,$_,3)} map {$_*3} 0..int((length($a)-1)/3);',    
    'Name5' => '$a="123456789" x 1000;$c=join ",",$a=~m/(.{1,3})/g;', 
    'Name6' => '$a="123456789" x 1000;$c=join ",",unpack("(a3)*",$a);',
};
```