Pages

Monday, 16 January 2017

Chapter 20 Exercise 6, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

20.6 (Use iterators on linked lists)
Write a test program that stores 5 million integers in a linked
list and test the time to traverse the list using an iterator vs.
using the get(index) method.

public class StopWatch {

    private long mStartTime;
    private long mEndTime;
    private long mElapsedPause;
    private int mLastSecond = 0;

    private boolean mIsOn;
    private boolean mIsPaused;

    private int mSeconds;
    private int mMinutes;
    private int mHours;

    public StopWatch() {
        mStartTime = System.currentTimeMillis();
    }

    public long getStartTime() {
        return mStartTime;
    }

    public long getEndTime() {
        return mEndTime;
    }

    public void start() {
        mIsOn = true;
        mStartTime = System.currentTimeMillis();
    }

    public void stop(){
        mEndTime = System.currentTimeMillis();
        mIsOn = false;
    }

    public long getElapsedTime() {
        return mEndTime - mStartTime;
    }

    public long peek() {
        return System.currentTimeMillis() - mStartTime;
    }
    public void pause() {
        mIsPaused = true;
        mElapsedPause = System.currentTimeMillis() - mStartTime;
    }

    public void resume() {
        mIsPaused = false;
        mStartTime = System.currentTimeMillis() - mElapsedPause;
    }

    public boolean isOn() {
        return mIsOn;
    }
    public boolean nextSecond() {
        updateTime();
        if (mSeconds != mLastSecond) {
            mLastSecond = mSeconds;
            return true;
        } else {
            return false;
        }
    }
    public boolean nextFiveSeconds() {
        updateTime();
        return mSeconds % 5 == 0;
    }

    public int getHour(){
        updateTime();
        return mHours;
    }

    public int getMinute(){
        updateTime();
        return mMinutes;
    }

    public int getSeconds(){
        updateTime();
        return mSeconds;
    }

    private void updateTime() {

        long currentTime = peek() / 1000;
        mSeconds = (int)(currentTime % 60);
        currentTime = currentTime / 60;

        mMinutes = (int) (currentTime % 60);
        currentTime = currentTime / 60;

        mHours = (int)(currentTime % 24);

    }

    @Override
    public String toString() {

        updateTime();
        String hours = getTimeFormat(mHours);
        String minutes = getTimeFormat(mMinutes);
        String seconds = getTimeFormat(mSeconds);

        return hours + ":" + minutes + ":" + seconds;
    }

    private String getTimeFormat(int time) {
        return (time > 9) ? time + "" : "0" + time;
    }

    public void reset(){
        stop();
        mHours = 0;
        mMinutes = 0;
        mSeconds = 0;
        mStartTime = 0;
        mEndTime = 0;
    }


    public boolean isPaused() {
        return mIsPaused;
    }
}

import java.util.Iterator;
import java.util.LinkedList;

public class Exercise_06 {

    public static void main(String[] args) {

        StopWatch stopWatch = new StopWatch();

        LinkedList<Integer> list = new LinkedList<>();
        for (int i = 0; i < 5000000; i++) {
            list.add(i);
        }
        Iterator<Integer> iterator = list.iterator();
        System.out.println("Start iterator");
        stopWatch.start();
        while (iterator.hasNext()) {
            iterator.next();
        }
        stopWatch.stop();
        System.out.println("End iterator");
        System.out.println("total time: " + stopWatch.getElapsedTime());

        stopWatch.reset();
        System.out.println("Start .get()");
        stopWatch.start();
        for (int i = 0; i < 5000000; i++) {
            list.get(i);
        }
        stopWatch.stop();
        System.out.println("End .get");
        System.out.println("total time: " + stopWatch.getElapsedTime());

        stopWatch.reset();
    }
}

No comments :

Post a Comment