##Style
Style
##Algorithm
Algorithm
##Update - Performance
Update - Performance
##Style
##Algorithm
##Update - Performance
Style
Algorithm
Update - Performance
- it won't recurseuse recursion for negatives, but instead do some more complicated sign-juggling
- it keeps everything as primitives (chars, etc.) instead of Strings
- it pre-computes the output sizes, etc.
Task NumberPad -> OP: (Unit: MILLISECONDS)
Count : 10000 Average : 0.2202
Fastest : 0.1960 Slowest : 9.8499
95Pctile : 0.2927 99Pctile : 0.4620
TimeBlock : 0.265 0.222 0.222 0.215 0.218 0.217 0.217 0.208 0.206 0.211
Histogram : 9769 218 7 1 4 1
Task NumberPad -> RL: (Unit: MILLISECONDS)
Count : 10000 Average : 0.3524
Fastest : 0.3127 Slowest : 15.0630
95Pctile : 0.5521 99Pctile : 0.6260
TimeBlock : 0.416 0.368 0.360 0.340 0.366 0.347 0.330 0.334 0.328 0.335
Histogram : 9900 88 6 2 3 1
Task NumberPad -> RLP: (Unit: MILLISECONDS)
Count : 10000 Average : 0.1866
Fastest : 0.1683 Slowest : 5.9859
95Pctile : 0.2280 99Pctile : 0.4241
TimeBlock : 0.218 0.189 0.193 0.181 0.185 0.185 0.177 0.178 0.179 0.182
Histogram : 9826 165 3 2 2 2
Task NumberPad -> OP: (Unit: MILLISECONDS)
Count : 10000 Average : 0.2202
Fastest : 0.1960 Slowest : 9.8499
95Pctile : 0.2927 99Pctile : 0.4620
TimeBlock : 0.265 0.222 0.222 0.215 0.218 0.217 0.217 0.208 0.206 0.211
Histogram : 9769 218 7 1 4 1
Task NumberPad -> RL: (Unit: MILLISECONDS)
Count : 10000 Average : 0.3524
Fastest : 0.3127 Slowest : 15.0630
95Pctile : 0.5521 99Pctile : 0.6260
TimeBlock : 0.416 0.368 0.360 0.340 0.366 0.347 0.330 0.334 0.328 0.335
Histogram : 9900 88 6 2 3 1
Task NumberPad -> RLP: (Unit: MILLISECONDS)
Count : 10000 Average : 0.1866
Fastest : 0.1683 Slowest : 5.9859
95Pctile : 0.2280 99Pctile : 0.4241
TimeBlock : 0.218 0.189 0.193 0.181 0.185 0.185 0.177 0.178 0.179 0.182
Histogram : 9826 165 3 2 2 2
- it won't recurse for negatives, but instead do some more complicated sign-juggling
- it keeps everything as primitives (chars, etc.) instead of Strings
- it pre-computes the output sizes, etc.
Task NumberPad -> OP: (Unit: MILLISECONDS)
Count : 10000 Average : 0.2202
Fastest : 0.1960 Slowest : 9.8499
95Pctile : 0.2927 99Pctile : 0.4620
TimeBlock : 0.265 0.222 0.222 0.215 0.218 0.217 0.217 0.208 0.206 0.211
Histogram : 9769 218 7 1 4 1
Task NumberPad -> RL: (Unit: MILLISECONDS)
Count : 10000 Average : 0.3524
Fastest : 0.3127 Slowest : 15.0630
95Pctile : 0.5521 99Pctile : 0.6260
TimeBlock : 0.416 0.368 0.360 0.340 0.366 0.347 0.330 0.334 0.328 0.335
Histogram : 9900 88 6 2 3 1
Task NumberPad -> RLP: (Unit: MILLISECONDS)
Count : 10000 Average : 0.1866
Fastest : 0.1683 Slowest : 5.9859
95Pctile : 0.2280 99Pctile : 0.4241
TimeBlock : 0.218 0.189 0.193 0.181 0.185 0.185 0.177 0.178 0.179 0.182
Histogram : 9826 165 3 2 2 2
- it won't use recursion for negatives, but instead do some more complicated sign-juggling
- it keeps everything as primitives (chars, etc.) instead of Strings
- it pre-computes the output sizes, etc.
Task NumberPad -> OP: (Unit: MILLISECONDS)
Count : 10000 Average : 0.2202
Fastest : 0.1960 Slowest : 9.8499
95Pctile : 0.2927 99Pctile : 0.4620
TimeBlock : 0.265 0.222 0.222 0.215 0.218 0.217 0.217 0.208 0.206 0.211
Histogram : 9769 218 7 1 4 1
Task NumberPad -> RL: (Unit: MILLISECONDS)
Count : 10000 Average : 0.3524
Fastest : 0.3127 Slowest : 15.0630
95Pctile : 0.5521 99Pctile : 0.6260
TimeBlock : 0.416 0.368 0.360 0.340 0.366 0.347 0.330 0.334 0.328 0.335
Histogram : 9900 88 6 2 3 1
Task NumberPad -> RLP: (Unit: MILLISECONDS)
Count : 10000 Average : 0.1866
Fastest : 0.1683 Slowest : 5.9859
95Pctile : 0.2280 99Pctile : 0.4241
TimeBlock : 0.218 0.189 0.193 0.181 0.185 0.185 0.177 0.178 0.179 0.182
Histogram : 9826 165 3 2 2 2
##Update - Performance
Since the question was edited to include performance as a criteria, then the following code will do essentially the same as above, but with some performance tweaks:
- it won't recurse for negatives, but instead do some more complicated sign-juggling
- it keeps everything as primitives (chars, etc.) instead of Strings
- it pre-computes the output sizes, etc.
It is still referenced against the right-side of the output, and has the same basic concepts, just... faster. About twice as fast, and about 20% faster than the OP's...
public static String neatifyRLP(final long val, final char pad, final int span) {
final String raw = Long.toString(val);
final int signlen = val < 0 ? 1 : 0;
final int digitlen = raw.length() - signlen;
final char[] out = new char[signlen + digitlen + (digitlen - 1) / span];
int pos = out.length - 1;
int src = raw.length() - 1;
final int ospan = span + 1;
while (pos >= signlen) {
out[pos] = (out.length - pos) % ospan == 0 ? pad : raw.charAt(src--);
pos--;
}
if (val < 0) {
out[0] = '-';
}
return new String(out);
}
I am not at all certain that it is more readable, but, it is not horrible either.
Here are the performance numbers based on neatifying 1000 random long values:
Task NumberPad -> OP: (Unit: MILLISECONDS)
Count : 10000 Average : 0.2202
Fastest : 0.1960 Slowest : 9.8499
95Pctile : 0.2927 99Pctile : 0.4620
TimeBlock : 0.265 0.222 0.222 0.215 0.218 0.217 0.217 0.208 0.206 0.211
Histogram : 9769 218 7 1 4 1
Task NumberPad -> RL: (Unit: MILLISECONDS)
Count : 10000 Average : 0.3524
Fastest : 0.3127 Slowest : 15.0630
95Pctile : 0.5521 99Pctile : 0.6260
TimeBlock : 0.416 0.368 0.360 0.340 0.366 0.347 0.330 0.334 0.328 0.335
Histogram : 9900 88 6 2 3 1
Task NumberPad -> RLP: (Unit: MILLISECONDS)
Count : 10000 Average : 0.1866
Fastest : 0.1683 Slowest : 5.9859
95Pctile : 0.2280 99Pctile : 0.4241
TimeBlock : 0.218 0.189 0.193 0.181 0.185 0.185 0.177 0.178 0.179 0.182
Histogram : 9826 165 3 2 2 2
##Update - Performance
Since the question was edited to include performance as a criteria, then the following code will do essentially the same as above, but with some performance tweaks:
- it won't recurse for negatives, but instead do some more complicated sign-juggling
- it keeps everything as primitives (chars, etc.) instead of Strings
- it pre-computes the output sizes, etc.
It is still referenced against the right-side of the output, and has the same basic concepts, just... faster. About twice as fast, and about 20% faster than the OP's...
public static String neatifyRLP(final long val, final char pad, final int span) {
final String raw = Long.toString(val);
final int signlen = val < 0 ? 1 : 0;
final int digitlen = raw.length() - signlen;
final char[] out = new char[signlen + digitlen + (digitlen - 1) / span];
int pos = out.length - 1;
int src = raw.length() - 1;
final int ospan = span + 1;
while (pos >= signlen) {
out[pos] = (out.length - pos) % ospan == 0 ? pad : raw.charAt(src--);
pos--;
}
if (val < 0) {
out[0] = '-';
}
return new String(out);
}
I am not at all certain that it is more readable, but, it is not horrible either.
Here are the performance numbers based on neatifying 1000 random long values:
Task NumberPad -> OP: (Unit: MILLISECONDS)
Count : 10000 Average : 0.2202
Fastest : 0.1960 Slowest : 9.8499
95Pctile : 0.2927 99Pctile : 0.4620
TimeBlock : 0.265 0.222 0.222 0.215 0.218 0.217 0.217 0.208 0.206 0.211
Histogram : 9769 218 7 1 4 1
Task NumberPad -> RL: (Unit: MILLISECONDS)
Count : 10000 Average : 0.3524
Fastest : 0.3127 Slowest : 15.0630
95Pctile : 0.5521 99Pctile : 0.6260
TimeBlock : 0.416 0.368 0.360 0.340 0.366 0.347 0.330 0.334 0.328 0.335
Histogram : 9900 88 6 2 3 1
Task NumberPad -> RLP: (Unit: MILLISECONDS)
Count : 10000 Average : 0.1866
Fastest : 0.1683 Slowest : 5.9859
95Pctile : 0.2280 99Pctile : 0.4241
TimeBlock : 0.218 0.189 0.193 0.181 0.185 0.185 0.177 0.178 0.179 0.182
Histogram : 9826 165 3 2 2 2