0

時間をテストして、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);
        }
    }
}
4

2 に答える 2

0

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) );
  }
}
于 2013-02-21T02:35:15.337 に答える
0

私は通常、実行時の感覚を得るために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ポイントカットを使用して、たとえばSpringaopを使用したメソッド実行の開始時間と終了時間をログに記録できます。コードはここからです: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をブロックしているため、ボトルネックになる可能性があります-なぜコンソールにそれほど多くの印刷をしたいのですか?このテストには価値がありますか?

于 2013-02-21T02:11:01.617 に答える