首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何实现选择排序的基准测试方法?

如何实现选择排序的基准测试方法?
EN

Stack Overflow用户
提问于 2022-09-13 12:52:07
回答 1查看 31关注 0票数 -1

我尝试测量选择在代码中排序数组的执行时间。选择排序部分正在工作,但基准测试方法"time“甚至没有执行。我的“时间”方法有什么问题,如何让它执行和度量性能时间?

代码语言:javascript
复制
     public class Selection_Sort
{
        public static void main(String[] args)
        {
            // define an array
            int[] array = {12, 3, 7, 6, 2};
            int i;
            int j;
            int t;
            int smallestNumber;


            // run an outer loop i from 0 to array.length-1 to repeat the process of selection sort
            for(i = 0; i < array.length-1; i++)
            {
                // smallest number position
                smallestNumber = i;

                // run an inner loop j for selection sort from i+1 to array.length
                for(j = i + 1; j < array.length; j++)
                {
                    // now check if the value at array[j] is smaller than value at array[smallestNumber]
                    if(array[j] < array[smallestNumber])
                    {
                        // if the value is smaller, then store the value of j to smallestNumber
                        smallestNumber = j;
                    }
                }

                // outside the body of inner loop j check if array[i] > array[smallestNumber]. If yes then swap the numbers
                if(array[i] > array[smallestNumber])
                {
                    t = array[i];
                    array[i] = array[smallestNumber];
                    array[smallestNumber] = t;
                }
            }

            // print the sorted array
            System.out.print("Selection Sort:\n");
            for(i = 0; i < array.length; i++)
            {
                System.out.print(array[i]+" ");
            }
        }

    public static void time(String[] args)
    {
        /* … The code being measured starts … */
        long startTime = System.nanoTime();

        /* … The code being measured ends … */

        long endTime = System.nanoTime();

        // get the difference between the two nano time valuess
        long timeElapsed = endTime - startTime;

        System.out.println("Execution time in nanoseconds: " + timeElapsed);
        System.out.println("Execution time in milliseconds: " + timeElapsed / 1000000);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-13 13:23:42

您需要在开始时间和结束时间之间测量代码。一种方法是将要测量的代码部分放在不同的方法中,然后按如下方式调用:

代码语言:javascript
复制
public class Selection_Sort{
public static void sortArray(int[] array)
{

    int smallestNumber;
    int t;

    // run an outer loop i from 0 to array.length-1 to repeat the process of selection sort
    for(int i = 0; i < array.length-1; i++)
    {
        // smallest number position
        smallestNumber = i;

        // run an inner loop j for selection sort from i+1 to array.length
        for(int j = i + 1; j < array.length; j++)
        {
            // now check if the value at array[j] is smaller than value at array[smallestNumber]
            if(array[j] < array[smallestNumber])
            {
                // if the value is smaller, then store the value of j to smallestNumber
                smallestNumber = j;
            }
        }

        // outside the body of inner loop j check if array[i] > array[smallestNumber]. If yes then swap the numbers
        if(array[i] > array[smallestNumber])
        {
            t = array[i];
            array[i] = array[smallestNumber];
            array[smallestNumber] = t;
        }
    }

   
}

  public static void main(String[] args)
  {
      // define an array
      int[] array = {12, 3, 7, 6, 2};
      /* … The code being measured starts … */
      long startTime = System.nanoTime();

      
      sortArray( array);
      /* … The code being measured ends … */

      long endTime = System.nanoTime();

      // get the difference between the two nano time valuess
      long timeElapsed = endTime - startTime;

      System.out.println("Execution time in nanoseconds: " + timeElapsed);
      System.out.println("Execution time in milliseconds: " + timeElapsed / 1000000);
      
      // print the sorted array
      System.out.print("Selection Sort:\n");
      for(int i = 0; i < array.length; i++)
      {
          System.out.print(array[i]+" ");
      }
      
  }
      
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73703394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档