简体   繁体   中英

Need Help intrepretting the results of this program

So, I made a small program to test Multithreading in java and compare the time it takes to scale an array using a while loop and then creating multiple threads and running those threads. I'm unsure about then numbers I'm getting when the program finishes, so I was wondering if I made a boneheaded error at some point and messed something up to get very disparate numbers.

Code below:

    import java.util.Scanner;

    public class arrayScaling {

      public static void main(String[] args) throws InterruptedException {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the amount of number you want the program to generate:");
        int numOfNumbs = input.nextInt();
        int [] arrayForNumbers = new int [numOfNumbs];
        int [] newArrayForNumbers = new int [numOfNumbs];

        for (int i = 0; i < arrayForNumbers.length; i++) {
            arrayForNumbers[i] = (int) ((Math.random() * 25) + 1);
        }

        long startTime = System.nanoTime();
        for (int i = 0; i < arrayForNumbers.length; i++) {
            newArrayForNumbers[i] = newArrayForNumbers[i] * 3;

        }

        long endTime = System.nanoTime();
        System.out.println();

        long totalExecutionTime = endTime-startTime;

        System.out.println("Time it takes execute scaling is " + 
                totalExecutionTime + " nanoseconds");
        System.out.println();

        int numOfNumLeftOver = numOfNumbs % 5;
        int numOfNumDivided = numOfNumbs / 5;

        int [] temp = null;
        int [] temp2 = null;
        int [] temp3 = null;
        int [] temp4 = null;
        int [] temp5 = null;

        MyThread thread1 = new MyThread (numOfNumbs/5);
        MyThread thread2 = new MyThread (numOfNumbs/5);
        MyThread thread3 = new MyThread (numOfNumbs/5);
        MyThread thread4 = new MyThread (numOfNumbs/5);
        MyThread thread5;
        if (numOfNumLeftOver != 0) {
            numOfNumDivided = numOfNumDivided + numOfNumLeftOver;
            thread5 = new MyThread (numOfNumDivided);   
        }
        else {
            thread5 = new MyThread (numOfNumbs/5);
        }

        int tempNum = 0;
        for ( int i = 0; i < thread1.getArray().length; i ++) {
            temp = thread1.getArray();
            temp[tempNum] = arrayForNumbers[tempNum];
            tempNum++;
        }
        for ( int i = 0; i < thread2.getArray().length; i ++) {
            temp2 = thread2.getArray();
            temp2[i] = arrayForNumbers[tempNum];
            tempNum++;
        }
        for ( int i = 0; i < thread3.getArray().length; i ++) {
            temp3 = thread3.getArray();
            temp3[i] = arrayForNumbers[tempNum];
            tempNum++;
        }
        for ( int i = 0; i < thread4.getArray().length; i ++) {
            temp4 = thread4.getArray();
            temp4[i] = arrayForNumbers[tempNum];
            tempNum++;
        }
        for ( int i = 0; i < thread5.getArray().length; i ++) {
            temp5 = thread5.getArray();
            temp5[i] = arrayForNumbers[tempNum];
            tempNum++;
        }       
        thread1.setArray(temp);
        thread2.setArray(temp2);
        thread3.setArray(temp3);
        thread4.setArray(temp4);
        thread5.setArray(temp5);

        long startTime2 = System.nanoTime();
        thread1.start(); 
        thread2.start(); 
        thread3.start(); 
        thread4.start();
        thread5.start();
        thread1.join();
        thread2.join();
        thread3.join();
        thread4.join();
        thread5.join();
        long endTime2 = System.nanoTime();

        long newTotalExecutionTime = endTime2 - startTime2;
        System.out.println("Time it takes execute scaling w/ multiple threads is " + 
                newTotalExecutionTime + " nanoseconds");

        if (newTotalExecutionTime < totalExecutionTime) {
            System.out.println("Multithreading was more effective");
        }
        else if (totalExecutionTime < newTotalExecutionTime) {
            System.out.println("The original algorithm was more effective");
        }
        else if (totalExecutionTime == newTotalExecutionTime) {
            System.out.println("Both method worked at the same speed");
        }
        input.close();
    }

}



    public class MyThread extends Thread {
    private int [] array;
    private int [] scaleArray;

    public MyThread(int size) {
        array = new int [size];
        scaleArray = new int [size];
    }

    public int[] getArray() {
        return array;
    }

    public void setArray(int[] array) {
        this.array = array;
    }

    public int[] getScaleArray() {
        return scaleArray;
    }

    public void setScaleArray(int[] scaleArray) {
        this.scaleArray = scaleArray;
    }

    public void run () {
        for (int z = 0; z < array.length; z++){
            scaleArray[z] = 3 * array[z];
        } 
    }

}

And the output of this program is:

Enter the amount of number you want the program to generate: 16

Time it takes execute scaling is 893 nanoseconds

Time it takes execute scaling w/ multiple threads is 590345 nanoseconds The original algorithm was more effective

Your results don't surprise me in the slightest. There's a lot of overhead to creating threads, starting them, waiting for them to finish and so on. Don't forget, 590345ns is still less than a millisecond; but most of that is to do with shuffling threads, not with multiplying the numbers.

If you want to see the threaded part of the program outperform the other part, try generating a whole lot more than 16 numbers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM