加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

与Groovy的字符串连接

发布时间:2020-12-14 16:39:35 所属栏目:大数据 来源:网络整理
导读:在Groovy中串联字符串的最好的(惯用的)方法是什么? 选项1: calculateAccountNumber(bank,branch,checkDigit,account) { bank + branch + checkDigit + account} 选项2: calculateAccountNumber(bank,account) { "$bank$branch$checkDigit$account"} 我在
在Groovy中串联字符串的最好的(惯用的)方法是什么?

选项1:

calculateAccountNumber(bank,branch,checkDigit,account) {
    bank + branch + checkDigit + account
}

选项2:

calculateAccountNumber(bank,account) {
    "$bank$branch$checkDigit$account"
}

我在旧的Groovy网站上创建了一个关于这个主题的有趣的点:你可以做的事情,但更好地离开。

As in Java,you can concatenate Strings with the “+” symbol. But Java
only needs that one of the two items of a “+” expression to be a
String,no matter if it’s in the first place or in the last one. Java
will use the toString() method in the non-String object of your “+”
expression. But in Groovy,you just should be safe the first item of
your “+” expression implements the plus() method in the right way,
because Groovy will search and use it. In Groovy GDK,only the Number
and String/StringBuffer/Character classes have the plus() method
implemented to concatenate strings. To avoid surprises,always use
GStrings.

解决方法

我总是去第二个方法(使用GString模板),虽然当有多个参数,像你有,我倾向于将它们包装在$ {X},因为我发现它使它更可读。

在这些方法上运行一些基准(使用Nagai Masato的优秀GBench module)也显示模板比其他方法更快:

@Grab( 'com.googlecode.gbench:gbench:0.3.0-groovy-2.0' )
import gbench.*

def (foo,bar,baz) = [ 'foo','bar','baz' ]
new BenchmarkBuilder().run( measureCpuTime:false ) {
  // Just add the strings
  'String adder' {
    foo + bar + baz
  }
  // Templating
  'GString template' {
    "$foo$bar$baz"
  }
  // I find this more readable
  'Readable GString template' {
    "${foo}${bar}${baz}"
  }
  // StringBuilder
  'StringBuilder' {
    new StringBuilder().append( foo )
                       .append( bar )
                       .append( baz )
                       .toString()
  }
  'StringBuffer' {
    new StringBuffer().append( foo )
                      .append( bar )
                      .append( baz )
                      .toString()
  }
}.prettyPrint()

这给我在我的机器上的以下输出:

Environment
===========
* Groovy: 2.0.0
* JVM: Java HotSpot(TM) 64-Bit Server VM (20.6-b01-415,Apple Inc.)
    * JRE: 1.6.0_31
    * Total Memory: 81.0625 MB
    * Maximum Memory: 123.9375 MB
* OS: Mac OS X (10.6.8,x86_64) 

Options
=======
* Warm Up: Auto 
* CPU Time Measurement: Off

String adder               539
GString template           245
Readable GString template  244
StringBuilder              318
StringBuffer               370

所以有可读性和速度在它的青睐,我建议模板;-)

注意:如果你添加toString()到GString方法的结尾,使输出类型与其他指标相同,并使其更公平的测试,StringBuilder和StringBuffer打败了GString方法的速度。然而,由于GString可以用于替代String的大多数事情(你只需要谨慎使用Map键和SQL语句),它大多可以没有这个最终转换

添加这些测试(如在评论中提出的)

'GString template toString' {
    "$foo$bar$baz".toString()
  }
  'Readable GString template toString' {
    "${foo}${bar}${baz}".toString()
  }

现在我们得到的结果:

String adder                        514
GString template                    267
Readable GString template           269
GString template toString           478
Readable GString template toString  480
StringBuilder                       321
StringBuffer                        369

所以你可以看到(如我所说),它比StringBuilder或StringBuffer慢,但仍然比添加字符串快一点…

但仍然更多的可读性。

编辑下面的ruralcoder的注释

更新到最新的gbench,更大的字符串用于连接,测试使用StringBuilder初始化为良好的大小:

@Grab( 'org.gperfutils:gbench:0.4.2-groovy-2.1' )

def (foo,baz) = [ 'foo' * 50,'bar' * 50,'baz' * 50 ]
benchmark {
  // Just add the strings
  'String adder' {
    foo + bar + baz
  }
  // Templating
  'GString template' {
    "$foo$bar$baz"
  }
  // I find this more readable
  'Readable GString template' {
    "${foo}${bar}${baz}"
  }
  'GString template toString' {
    "$foo$bar$baz".toString()
  }
  'Readable GString template toString' {
    "${foo}${bar}${baz}".toString()
  }
  // StringBuilder
  'StringBuilder' {
    new StringBuilder().append( foo )
                       .append( bar )
                       .append( baz )
                       .toString()
  }
  'StringBuffer' {
    new StringBuffer().append( foo )
                      .append( bar )
                      .append( baz )
                      .toString()
  }
  'StringBuffer with Allocation' {
    new StringBuffer( 512 ).append( foo )
                      .append( bar )
                      .append( baz )
                      .toString()
  }
}.prettyPrint()

给出

Environment
===========
* Groovy: 2.1.6
* JVM: Java HotSpot(TM) 64-Bit Server VM (23.21-b01,Oracle Corporation)
    * JRE: 1.7.0_21
    * Total Memory: 467.375 MB
    * Maximum Memory: 1077.375 MB
* OS: Mac OS X (10.8.4,x86_64)

Options
=======
* Warm Up: Auto (- 60 sec)
* CPU Time Measurement: On

                                    user  system  cpu  real

String adder                         630       0  630   647
GString template                      29       0   29    31
Readable GString template             32       0   32    33
GString template toString            429       0  429   443
Readable GString template toString   428       1  429   441
StringBuilder                        383       1  384   396
StringBuffer                         395       1  396   409
StringBuffer with Allocation         277       0  277   286

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读