Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.List;

import java.util.Set;

public class Main {
    public static long populate(List list, int size) {
        long start, stop, result = 0;
        for (int j = 0; j < 100; j++) {
            list.clear();
            start = System.nanoTime();
            for (int i = 0; i < size; i++) {
                list.add(i);
            }
            stop = System.nanoTime();
            result += stop - start;
        }
        return result / 100;
    }

    public static long populate(Set set, int size) {
        long start, stop, result = 0;
        for (int i = 0; i < 100; i++) {
            set.clear();
            start = System.nanoTime();
            for (int j = 0; j < size; j++) {
                set.add(j);
            }
            stop = System.nanoTime();
            result += stop - start;
        }
        return result / 100;
    }

    public static long add(List list) {
        long start, stop, result = 0;
        for (int i = 0; i < 100; i++) {
            start = System.nanoTime();
            list.add(list.size() + 1 + i);
            stop = System.nanoTime();
            result += stop - start;
        }
        return result / 100;
    }

    public static long add(Set set) {
        long start, stop, result = 0;
        for (int i = 0; i < 100; i++) {
            start = System.nanoTime();
            set.add(set.size() + 1 + i);
            stop = System.nanoTime();
            result += stop - start;
        }
        return result / 100;
    }
}