使用方法所花费的时间

时间:2020-09-20 17:31:18

标签: java list random append

我正在尝试查找将0到200之间的随机整数插入整数列表所花费的时间。我不完全确定如何将随机数限制在0到200之间,而我的另一个问题是出现错误:

java.lang.IllegalArgumentException:绑定必须为正

实验控制器:

public class ExperimentController
{
    
    /**
     * Constructor for objects of class ExperimentController
     */
    public static void main(String[] args)
    {
        ExperimentController EX = new ExperimentController();

        for(int i =1; i<=200;i=i+50){
          long time = EX.timeAppend(i, 0);
          System.out.println(time);
        }
    }

    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y  a sample parameter for a method
     * @return    the sum of x and y
     */
    public long timeAppend(int numberOfItems, int seed){
        long startTime = System.nanoTime();
        
        IntegerList list = new IntegerList();
        Random random = new Random(seed);
        
        for(int i=0; i<numberOfItems; i++){
            int randomInt = random.nextInt(seed);            
            list.append(randomInt);   
        }
        

        long stopTime = System.nanoTime();
        long timeTotal = stopTime-startTime;
        return timeTotal;
        
    
    }   
}

整数列表:

public class IntegerList extends IntegerListADT
{
    Cell root;

    /**
     * Constructor for objects of class IntegerList
     */
    public static void main(String[] args)
    {
         Cell root;
   
    }
    
    public void append(int x){
      if(root == null){
          root = new Cell(x);
        }
        
        else{
            root.append(x);    
        }
        
    }
    
    public String toString(){
        if(root == null){
            return  root.toString();    
        }
        else{
            return root.toString();
        }
        
    }
    
    public boolean isEmpty(){
        if(root == null){
           return true;
        }
        else{
           return false;
        }
    }

}

单元格类别:

public class Cell
{
    // instance variables - replace the example below with your own
    private int val;
    private Cell next;
     

    /**
     * Constructor for objects of class Cell
     */
    public Cell(int val)
    {
       
    }

    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y  a sample parameter for a method
     * @return    the sum of x and y
     */
    public void append(int x)
    {
       if (next == null) {
           next = new Cell(x);
        }
        
        else{
         next.append(x);   
        }
        
    }
    
    public String toString(){
        if(next == null){
            return String.valueOf(val);
        }
        return val+ next.toString();
    }
}

2 个答案:

答案 0 :(得分:0)

要花费时间,需要一种方法,您只需要对逻辑数学有一些了解。您需要在方法执行之前时间和在方法执行之后时间。用方法前的时间减去方法后的时间。

如何获取持续时间

public static void main(String[] args) {
    final long timeBefore = System.currentTimeMillis(); // Could be replaced with System.nanoTime(), if you want to get the nanos
    method(); // Your method
    final long duration = System.currentTimeMillis() - timeBefore;
}

如何限制随机数

如果要限制随机使用,则必须在构造函数中提供种子:

random.nextInt(200);

答案 1 :(得分:0)

我用

public static final Random  RANDOM = new Random( Calendar.getInstance().getTimeInMillis() );
.
.
.
public static int getRandomNumber( int start, int end ) throws IllegalArgumentException
    {
        int range;

        if (end > start)
            range = end - start;
        else
            range = start - end;

        if (range > 0)
            return RANDOM.nextInt( range ) + start;

        throw new IllegalArgumentException( "Range is not positive: " + range );
    }

因此您将使用

long time = MyClass.getRandomNumber( 0, 200 );