【问题标题】:Time taken to use a method使用方法所花费的时间
【发布时间】:2020-09-20 17:31:18
【问题描述】:

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

java.lang.IllegalArgumentException: bound 必须是正数

实验控制器:

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

【问题讨论】:

    标签: java list random append


    【解决方案1】:

    要获得时间,所需的方法,您只需要一些逻辑数学理解。您采用 时间之前 方法和 时间之后 方法执行。方法之后的时间减去方法之前的时间。

    如何获取时长

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

    【讨论】:

      【解决方案2】:

      我用

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-12
        • 1970-01-01
        • 2013-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-28
        相关资源
        最近更新 更多