lanx@nc10-ubuntu:~$ python Python 2.5.2 (r252:60911, Jan 20 2010, 23:16:55) ... >>> str="123" >>> str+="4" >>> str '1234'
The only thing I can think of are runtime issues :
I've read that in JS str+="a";str+="b" takes 40% longer than str=str+"a"+"b", because strings have to be copied...
But in perl the second version needs much longer...
DB<1> $str="";$t=time;for (1..100000) {$str=$str."a"."b"};print time +-$t 13 DB<2> $str="";$t=time;for (1..1000000) {$str.="a";$str.="b"};print t +ime-$t 1
Actually the same code in python has similar benchmarks
>>> for i in range(1, 1000000): ... str+="a"+"b" ... >>> str="" >>> for i in range(1, 100000): ... str=str+"a"+"b" ...
So where is the practical difference?
Cheers Rolf
UPDATE: Maybe not a good benchmark, since the growing of $str plays a too important role ...
DB<1> $t=time;for (1..10000000) {$str="";$str.="a"."b";};print time- +$t 10 DB<2> $t=time;for (1..10000000) {$str="";$str=$str."a";$str.="b";};p +rint time-$t 17 DB<3> $t=time;for (1..10000000) {$str="";$str=$str."a"."b";};print t +ime-$t 15
In reply to Re: programming languages and immutable data types
by LanX
in thread programming languages and immutable data types
by mattk1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |