- 论坛徽章:
- 1
|
关于perl字符串连接的方法讨论
- #!/usr/bin/perl
- use Benchmark qw(:all) ;
- timethese(20, {
- 'f1' =>; \&compute1,
- 'f2' =>; \&compute2
- });
- sub compute1(){
- for($i=0;$i<10000;$i++){
- $tmp = $tmp."aaa";
- }
- }
- sub compute2(){
- for($i=0;$i<10000;$i++){
- $tmp .= "aaa";
- }
- }
复制代码Benchmark: timing 20 iterations of f1, f2...
f1: 97 wallclock secs (96.93 usr + 0.24 sys = 97.17 CPU) @ 0.21/s (n=20)
f2: 1 wallclock secs ( 0.29 usr + 0.15 sys = 0.44 CPU) @ 45.45/s (n=20)
的確比較快....
為什麼?
我猜可能跟pointer取值、運算、給值的次數有關係....
因為 perl取得變量的內容..採用的是二次pointer...
也就是
變量A指向位址B、位址B指向實際內存中的位址...
所以程序中多一個的變量..就會多一次pointer取值...
詳情可能得看perl的source才能知道...:) |
|