public int[] solution1(long n) {
String str = n + "";
int[] answer = new int[str.length()];
for (int i = 0; i < answer.length; i++) {
answer[i] = (int) (n % 10);
if (n < 10) break;
n /= 10;
}
return answer;
}
public int[] solution2(long n) {
return new StringBuilder().append(n).reverse().chars()
.map(Character::getNumericValue).toArray();
}
void solution1_solution2(){
long number = 1234567890123456L;
int iterations = 100000;
long startTime1 = System.nanoTime();
for (int i = 0; i < iterations; i++) {
solution1(number);
}
long endTime1 = System.nanoTime();
long startTime2 = System.nanoTime();
for (int i = 0; i < iterations; i++) {
solution2(number);
}
long endTime2 = System.nanoTime();
System.out.println("Solution1 : " + (endTime1 - startTime1) + " nanoseconds");
System.out.println("Solution2 : " + (endTime2 - startTime2) + " nanoseconds");
}