Created
April 24, 2018 21:19
-
-
Save mikhailian/91ff9af732522726778c53a47880f090 to your computer and use it in GitHub Desktop.
comparemyass
This file contains hidden or 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
import java.util.Random; | |
public class LongCompare { | |
static Random r = new Random(); | |
static int compareLikeLong(long x, long y) { | |
return (x < y) ? -1 : ((x == y) ? 0 : 1); | |
} | |
static int compareBadass(long x, long y) { | |
if (x > y) return 1; | |
else return (int) (y - x >> 63); | |
} | |
public static void main(String... args) { | |
long[] longs = new long[Integer.MAX_VALUE / 1024]; | |
for (int i = 0; i < Integer.MAX_VALUE / 1024; i++) { | |
longs[i] = r.nextLong(); | |
} | |
long startTime = System.nanoTime(); | |
for (int i = 0; i < Integer.MAX_VALUE / 1024 - 1; i++) { | |
compareLikeLong(longs[i], longs[i++]); | |
} | |
long endTime = System.nanoTime(); | |
System.out.println("time elapsed: " + (endTime - startTime)); | |
startTime = System.nanoTime(); | |
for (int i = 0; i < Integer.MAX_VALUE / 1024 - 1; i++) { | |
compareBadass(longs[i], longs[i++]); | |
} | |
endTime = System.nanoTime(); | |
System.out.println("time elapsed: " + (endTime - startTime)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment