0

I want to test the time and see how long it will take to display like 3 millions number, but I don't know how I can do that. Any ideas will be appreciated.

import java.util.*;

public class LinkedListProgram {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        LinkedList<Integer> list = new LinkedList<Integer>();
        Random randomNumbers = new Random();

        System.out.println("Enter number of integers");
        int number = input.nextInt();
        for (int i=0; i < number; i++){
            list.add(randomNumbers.nextInt(100));
        }

        for (Iterator i = list.iterator(); i.hasNext();) {
            Integer integer = (Integer) i.next();
            System.out.println(integer);
        }
    }
}

2 Answers2

0

I usually use junit or ngunit to get a feel for runtime. It will tell you how long it took for the test to run in your ide separate from the console.

Or you can just log the start time and end time like so:

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));

doStuff();

date = new Date();
System.out.println(dateFormat.format(date));

Or you can log the execution time:

                    long start = System.currentTimeMillis();
                    System.out.println("Going to call the method.");

                    doStuff();

System.out.println("Method execution completed.");
                    long elapsedTime = System.currentTimeMillis() - start;
                    System.out.println("Method execution time: " + elapsedTime + " milliseconds.");

You could use log4j if you want to write somewhere else (ie to a file) instead of get it all mixed up in the console

logger.info(dateFormat.format(date);

If you want to get fancier you can use AOP pointcuts to log start and end time around method execution with spring aop for example. Code is from here: http://veerasundar.com/blog/2010/01/spring-aop-example-profiling-method-execution-time-tutorial/

@Aspect
public class BusinessProfiler {

        @Pointcut("execution(* com.veerasundar.spring.aop.*.*(..))")
        public void businessMethods() { }

        @Around("businessMethods()")
        public Object profile(ProceedingJoinPoint pjp) throws Throwable {
                long start = System.currentTimeMillis();
                System.out.println("Going to call the method.");
                Object output = pjp.proceed();
                System.out.println("Method execution completed.");
                long elapsedTime = System.currentTimeMillis() - start;
                System.out.println("Method execution time: " + elapsedTime + " milliseconds.");
                return output;
        }

}

Printing to the console may be a bottleneck as it's blocking io - why do you want to print so much to the console? Is there value in this test?

JasonG
  • 5,794
  • 4
  • 39
  • 67
0

If you are interested in examining the performance of LinkedList and possibly comparing it to, say, ArrayList or regular arrays, then you might want to remove the println statements. The output to the display takes far more time than moving the data around in memory.

import java.util.*;

public class LinkedListProgram {

 public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    LinkedList<Integer> list = new LinkedList<Integer>();
    Random randomNumbers = new Random();

    System.out.println("Enter number of integers");
    int number = input.nextInt();
    long start = System.currentTimeMillis();
    for (int i=0; i < number; i++){
        list.add(randomNumbers.nextInt(100));
    }
    long generateTime = System.currentTimeMillis();
    int sum=0;
    for (int x : list) {
        sum += x
    }
    long endTime = System.currentTimeMillis();

    System.out.println("Time to gernerate numbers: " +  (generateTime - start) );
    System.out.println("Time to sum list: " +  (endTime  - generateTime) );
  }
}
Thorn
  • 4,015
  • 4
  • 23
  • 42