Friends, I can not understand the reason for the long work cycle. So, I create a cycle.
for (int i = 1; i < Integer.MAX_VALUE; i++) { // Вычисляем количество цифр в числе i int length = (int)Math.ceil(Math.log10(i)); } The whole program works like this 10ms and ends successfully. Such a program works 3ms:
for (int i = 1; i < Integer.MAX_VALUE; i++) { // Создаем массив длиной 10 (количество цифр в Integer.MAX_VALUE) int[] digits = new int[10]; } But this program hangs for a long time:
for (int i = 1; i < Integer.MAX_VALUE; i++) { int length = (int)Math.ceil(Math.log10(i)); int[] digits = new int[length]; } Why? After all, the value of length will always be less than or equal to 10. And I can not understand. After all, this program works only 12ms:
for (int i = 1; i < Integer.MAX_VALUE; i++) { int length = (int)Math.ceil(Math.log10(i)); int[] digits = new int[10]; }
Math.ceilcall is not optimized at all (you can not even create an array). IfMath.ceilreplaced by a caste toint, then everything ends successfully. - zRrr