Last active
November 28, 2016 17:30
-
-
Save kostapc/277931819957ae0e43c8aa3221638431 to your computer and use it in GitHub Desktop.
test math and string versions of long truncation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LongNumberTruncate { | |
public static void main(String[] args) { | |
long[] values = new long[8]; | |
values[0] = 1234567891234567891L; // 16 | |
values[1] = 123456789223456789L; // 15 | |
values[2] = 123456789323456L; // 14 | |
values[3] = 1234567894236L; // 13 | |
values[4] = 1234567895L; // 10 | |
values[5] = 123456789L; // 9 | |
values[6] = 1234567L; // 7 | |
values[7] = 12345L; // 5 | |
for (long value : values) { | |
long time; | |
long nv; | |
time = System.nanoTime(); | |
nv = base10ByMath(value); | |
time = System.nanoTime() - time; | |
print("match ", time, value, nv); | |
// -------------------------- | |
time = System.nanoTime(); | |
nv = base10ByString(value); | |
time = System.nanoTime() - time; | |
print("string", time, value, nv); | |
} | |
} | |
private static void print(String prefix, long time, long value, long nv) { | |
System.out.println(String.format( | |
"%s -> %s\n"+ | |
"orig (%s): %s\n"+ | |
"new (%s): %s\n", | |
prefix, time, | |
String.valueOf(value).length(), value, | |
String.valueOf(nv).length(), nv | |
)); | |
} | |
private static final int lbs = 10; | |
private static long base10ByString(long x) { | |
String value = String.valueOf(x); | |
if(value.length()<lbs) { | |
int diff = lbs-value.length(); | |
for(int i=0; i<diff;i++) { | |
value += 0; | |
} | |
return Long.valueOf(value); | |
} | |
value = value.substring(0,lbs); | |
return Long.valueOf(value); | |
} | |
private static final long base = 1000000000L; | |
private static final int lb = (int)Math.ceil(Math.log10(base))+1; | |
private static long base10ByMath(long x) { | |
if(x<0) { | |
return base; | |
} | |
int l = (int) Math.ceil(Math.log10(x)); | |
long pow; | |
if(l<lb) { | |
pow = lb - l; | |
return (long) (x * Math.pow(10, pow)); | |
} else { | |
pow = (long) Math.pow(10, l - lb); | |
return x / pow; | |
} | |
} | |
/* | |
match -> 8086 | |
orig (19): 1234567891234567891 | |
new (10): 1234567891 | |
string -> 35455 | |
orig (19): 1234567891234567891 | |
new (10): 1234567891 | |
match -> 4354 | |
orig (18): 123456789223456789 | |
new (10): 1234567892 | |
string -> 9019 | |
orig (18): 123456789223456789 | |
new (10): 1234567892 | |
match -> 1865 | |
orig (15): 123456789323456 | |
new (10): 1234567893 | |
string -> 9019 | |
orig (15): 123456789323456 | |
new (10): 1234567893 | |
match -> 2488 | |
orig (13): 1234567894236 | |
new (10): 1234567894 | |
string -> 9641 | |
orig (13): 1234567894236 | |
new (10): 1234567894 | |
match -> 2488 | |
orig (10): 1234567895 | |
new (10): 1234567895 | |
string -> 8086 | |
orig (10): 1234567895 | |
new (10): 1234567895 | |
match -> 2177 | |
orig (9): 123456789 | |
new (10): 1234567890 | |
string -> 77440 | |
orig (9): 123456789 | |
new (10): 1234567890 | |
match -> 2177 | |
orig (7): 1234567 | |
new (10): 1234567000 | |
string -> 10885 | |
orig (7): 1234567 | |
new (10): 1234567000 | |
match -> 1866 | |
orig (5): 12345 | |
new (10): 1234500000 | |
string -> 11818 | |
orig (5): 12345 | |
new (10): 1234500000 | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment