【问题标题】:Test time to traverse a list of integers [duplicate]遍历整数列表的测试时间[重复]
【发布时间】:2013-02-21 02:02:37
【问题描述】:

我想测试时间,看看显示 300 万个数字需要多长时间,但我不知道该怎么做。任何想法将不胜感激。

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);
        }
    }
}

【问题讨论】:

标签: java iterator linked-list


【解决方案1】:

我通常使用 junit 或 ngunit 来了解运行时。它会告诉您测试在您的 ide 中运行与控制台分开需要多长时间。

或者您可以像这样记录开始时间和结束时间:

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));

或者你可以记录执行时间:

                    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.");

如果您想在其他地方(即文件)写入而不是在控制台中将所有内容混在一起,您可以使用 log4j

logger.info(dateFormat.format(date);

如果你想变得更高级,你可以使用 AOP 切入点来记录方法执行的开始和结束时间,例如使用 spring aop。代码来自这里: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;
        }

}

打印到控制台可能是一个瓶颈,因为它阻塞了 io - 为什么要在控制台上打印这么多内容?这个测试有价值吗?

【讨论】:

    【解决方案2】:

    如果您有兴趣检查 LinkedList 的性能并可能将其与 ArrayList 或常规数组进行比较,那么您可能需要删除 println 语句。输出到显示器所花费的时间比在内存中移动数据要多得多。

    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) );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-25
      • 1970-01-01
      • 2014-07-06
      • 1970-01-01
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-26
      相关资源
      最近更新 更多