java计算程序运行时间的方法之一

可以使用Date类,下面的代码是直接调用函数System.currentTimeMillis()来获取当前系统时间,单位为毫秒。

所以只需要在需要测试的代码前后分别获取一次系统时间,然后差值就是该段代码执行的时间了。

Scanner in = new Scanner(System.in);
		int a = in.nextInt();
		long start = System.currentTimeMillis();
		switch(a)
		{
		case 0:
			System.out.println(Color.red);
			break;
		case 1:
			System.out.println(Color.green);
			break;
		default:
			System.out.println(Color.black);
			break;
		}
		long time = System.currentTimeMillis() - start;
		System.out.println(time);


posted @ 2016-06-03 14:45  Victor_Lv  阅读(275)  评论(0编辑  收藏  举报