improvements (#521)

- inline computeIfAbsent
- replace arraycopy by copyOfRange

Co-authored-by: Yann Moisan <yann@zen.ly>
This commit is contained in:
Yann Moisan 2024-01-23 19:58:10 +01:00 committed by GitHub
parent c886aaba34
commit b3420d9348
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -104,10 +104,7 @@ public class CalculateAverage_YannMoisan {
break;
field[fieldCurrentIndex++] = fieldByte;
}
var dst = new byte[fieldCurrentIndex];
System.arraycopy(field, 0, dst, 0, fieldCurrentIndex);
var fieldStr = new Location(dst);
// System.arraycopy(field, 0, dst, 0, fieldCurrentIndex);
var fieldStr = new Location(Arrays.copyOfRange(field, 0, fieldCurrentIndex));
var number = 0;
var sign = 1;
while (bb.position() < limit) {
@ -119,9 +116,15 @@ public class CalculateAverage_YannMoisan {
else if (numberByte != '.')
number = number * 10 + (numberByte - '0');
}
stats.computeIfAbsent(fieldStr,
k -> new Stat())
.update(sign * number);
var v = stats.get(fieldStr);
if (v == null) {
var vv = new Stat();
vv.update(sign * number);
stats.put(fieldStr, vv);
}
else {
v.update(sign * number);
}
}
return stats;