Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static final int ITERATIONS = 100000000;
- public static void main(String[] arguments) {
- final int iterations = 20;
- long optionOneTotal = 0;
- long optionTwoTotal = 0;
- for (int i = 0; i < iterations; i++) {
- optionOneTotal += optionOne();
- optionTwoTotal += optionTwo();
- }
- System.out.println("OptionOne time: " + (optionOneTotal / iterations));
- System.out.println("OptionTwo time: " + (optionTwoTotal / iterations));
- }
- private static long optionOne() {
- long start = System.nanoTime();
- int n = 0;
- for (int i = 0; i < ITERATIONS; i++) {
- n += 2 * i * i;
- }
- System.out.println(n);
- return System.nanoTime() - start;
- }
- private static long optionTwo() {
- long start = System.nanoTime();
- int n = 0;
- for (int i = 0; i < ITERATIONS; i++) {
- n += 2 * (i * i);
- }
- System.out.println(n);
- return System.nanoTime() - start;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement