开始前 long s1 = System.currentTimeMillis() 结束后 long s2 = System.currentTimeMillis()System.out.prinltn(s2-s1)得到多少毫秒。
你到底是求时间还是时间复杂度?时间复杂度一般是看出来的吧 求时间可以用 long startTime = System.nanoTime(); // ... the code being measured ... long estimatedTime = System.nanoTime() - startTime; API里这段就是用来求代码运行时间的
package java04;import java.util.Arrays;public class A { public static void main(String args[]){ int[] array = new int[]{1,2,2,4,5,5,5,8,9,9}; System.out.println(Arrays.toString(filter(array))); } /* *主要思路就是先把结果放在原数组前面,并得到结果数组长度,最后复数组到结果 */ public static int[] filter(int[] array){
int temp = array[0]; int pos = 1; for(int i = 1;i<array.length;i++){ if(temp!=array[i]){ temp=array[i]; array[pos++]=temp;//在原数组不重复的按顺序放置元素 } } int[] result = new int[pos];//开辟新数组 System.arraycopy(array, 0, result, 0, pos);//拷贝原数组前部分到结果 return result; }} 如上面的代码,要想知道程序的运行时间应如何改代码呢/ 具体要添加到代码应加在什么地方.谢谢
import java.util.Arrays; public class A { public static void main(String args[]){ int[] array = new int[]{1,2,2,4,5,5,5,8,9,9}; long startTime = System.nanoTime(); System.out.println(Arrays.toString(filter(array))); long estimatedTime = System.nanoTime() - startTime; System.out.println(estimatedTime); } /* *主要思路就是先把结果放在原数组前面,并得到结果数组长度,最后复数组到结果 */ public static int[] filter(int[] array){
int temp = array[0]; int pos = 1; for(int i = 1;i <array.length;i++){ if(temp!=array[i]){ temp=array[i]; array[pos++]=temp;//在原数组不重复的按顺序放置元素 } } int[] result = new int[pos];//开辟新数组 System.arraycopy(array, 0, result, 0, pos);//拷贝原数组前部分到结果 return result; } }
long s1 = System.currentTimeMillis()
结束后
long s2 = System.currentTimeMillis()System.out.prinltn(s2-s1)得到多少毫秒。
求时间可以用
long startTime = System.nanoTime();
// ... the code being measured ...
long estimatedTime = System.nanoTime() - startTime;
API里这段就是用来求代码运行时间的
{
public static void main(String args[]){
int[] array = new int[]{1,2,2,4,5,5,5,8,9,9};
System.out.println(Arrays.toString(filter(array)));
}
/*
*主要思路就是先把结果放在原数组前面,并得到结果数组长度,最后复数组到结果
*/
public static int[] filter(int[] array){
int temp = array[0];
int pos = 1;
for(int i = 1;i<array.length;i++){
if(temp!=array[i]){
temp=array[i];
array[pos++]=temp;//在原数组不重复的按顺序放置元素
}
}
int[] result = new int[pos];//开辟新数组
System.arraycopy(array, 0, result, 0, pos);//拷贝原数组前部分到结果
return result; }}
如上面的代码,要想知道程序的运行时间应如何改代码呢/
具体要添加到代码应加在什么地方.谢谢
import java.util.Arrays; public class A
{
public static void main(String args[]){
int[] array = new int[]{1,2,2,4,5,5,5,8,9,9};
long startTime = System.nanoTime();
System.out.println(Arrays.toString(filter(array)));
long estimatedTime = System.nanoTime() - startTime;
System.out.println(estimatedTime);
}
/*
*主要思路就是先把结果放在原数组前面,并得到结果数组长度,最后复数组到结果
*/
public static int[] filter(int[] array){
int temp = array[0];
int pos = 1;
for(int i = 1;i <array.length;i++){
if(temp!=array[i]){
temp=array[i];
array[pos++]=temp;//在原数组不重复的按顺序放置元素
}
}
int[] result = new int[pos];//开辟新数组
System.arraycopy(array, 0, result, 0, pos);//拷贝原数组前部分到结果
return result; } }