Just tested this using the regex method and the String getAt method. getAt seems to be about 2x faster than regex over 10k
def input = "";
for(i=1;i<10000;i++)
{
input += "JOHN DOE 123 \n"
}
def fieldDefs = [firstName: 10, lastName: 10, someValue: 10]
def benchmark = { closure ->
start = System.currentTimeMillis()
closure.call()
now = System.currentTimeMillis()
now - start
}
def pattern = "^" + fieldDefs.collect { k, v -> "(.{$v})" }.join('') + "\$"
duration = benchmark {
rows = []
input.eachLine { line ->
String firstName = line.getAt(0..9).trim();
String lastName = line.getAt(10..19).trim();
String someValue = line.getAt(20..29).trim();
rows << ["firstName":firstName,"lastName":lastName,"someValue":someValue];
}
//println rows
}
println "execution of string method took ${duration} ms"
def duration = benchmark {
rows = []
input.eachLine { line ->
def m = line =~ pattern
if (m) {
def names = fieldDefs.keySet() as List
def values = m[0][1..-1].collect { it.trim() }
rows << [names, values].transpose().collectEntries{it}
}
}
//println rows
}
println "execution of regex method took ${duration} ms"
execution of string method took 245 ms
execution of regex method took 505 ms