【发布时间】:2010-08-03 11:39:51
【问题描述】:
class Test {
String field
int num
public Test (String field, int num) {
this.field = field
this.num = num
}
}
def start = System.currentTimeMillis()
def testObj = new Test("i'm field", 1)
println "Beans: ${System.currentTimeMillis() - start}"
def start2 = System.currentTimeMillis()
def map = [:]
map.field = "i'm field"
map.num = 1
println "Maps: ${System.currentTimeMillis() - start2}"
输出是:
Beans: 3
Maps: 0
我的 Grails 服务器通过 JSON 进行通信。我正在使用 map JSON 转换,但我认为使用 bean 会更好,因为在 maps 情况下,您必须调用许多 put() 方法...
但简单的脚本表明 Map 创建和两个 put 操作比简单的对象构造函数要快...
那么我会继续使用地图还是 bean 更可取?
【问题讨论】:
标签: groovy