排序时间总是与第一次排序不同 [英] Sorting time always different from first sort

查看:170
本文介绍了排序时间总是与第一次排序不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了几个排序算法。我想比较他们的排序时间,至少差不多。但是在第一次循环之后,所有排序时间都会减少,除了StoogeSort。我认为在背景上有所优化,但我应该考虑采取哪些措施?第一个还是其他?为什么会发生这种情况?

I wrote couple sorting algorithm. I want to compare their sorting times, at least nearly. But after first loop all sorting times decreasing except StoogeSort. I think something optimizating at background but which measure should I consider? First one or the others? And why is this happening?

    public static void main(String[] args) {
    RandomNumber rn = new RandomNumber();

    Scanner sc = new Scanner(System.in);
    while(true){
        System.out.println("Enter the input size.");
        int n = sc.nextInt();
        int[] experimentalArray = rn.experimentalArrayGenerator(n);



        Stopwatch sw1 = new Stopwatch();
        StoogeSort ss = new StoogeSort(experimentalArray.clone());
        System.out.println("StoogeSort : " + sw1.elapsedTime() + " µs");

        Stopwatch sw2 = new Stopwatch();
        RadixSort rs = new RadixSort(experimentalArray.clone());
        System.out.println("RadixSort : " + sw2.elapsedTime() + " µs");

        Stopwatch sw3 = new Stopwatch();
        ShakerSort shs = new ShakerSort(experimentalArray.clone());
        System.out.println("ShakerSort : " + sw3.elapsedTime() + " µs");

        Stopwatch sw4 = new Stopwatch();
        MaximumSubarray ms = new MaximumSubarray();
        int a = ms.maxSubArraySum(experimentalArray.clone());
        System.out.println("MaximumSubarray : " + sw4.elapsedTime() + " µs");
        System.out.println("------------------------------------------------------");
    }
}

4循环后的输出:

推荐答案

微观标记是一个复杂的因为许多因素会影响执行时间(例如,Jon Skeet在评论中指出的即时编译和垃圾收集)。

Microbenchmarking is a complicated matter as many factors influence the execution time (e.g. just-in-time compilation and garbage collection as noted by Jon Skeet in the comments).

你应该阅读本文档由Peter Sestoft提供,如果您想了解如何接近microbenchmarking。

You should read this document by Peter Sestoft if you want to gain an understanding of how you should approach microbenchmarking.

在这里引用文档的摘要,因为文档是外部资源:

Quoting the abstract of the document here, as the document is an external resource:


有时人们想测量软件的速度,例如,以b $ b衡量一种解决问题的新方法是否更快比旧的
一个。进行这样的时间测量和微基准测试需要花费大量资金,特别是在Java
虚拟机和Microsoft的公共语言基础结构(.NET)等托管平台上,
或者结果可能是任意的和误导性的。

Sometimes one wants to measure the speed of software, for instance, to measure whether a new way to solve a problem is faster than the old one. Making such time measurements and microbenchmarks requires considerable care, especially on managed platforms like the Java Virtual Machine and Microsoft’s Common Language Infrastructure (.NET), or else the results may be arbitrary and misleading.

这里我们给出一些关于运行微基准测试的
建议,特别是对于托管的
平台。大多数示例都是Java,但建议适用于在托管平台上执行的任何
语言,包括Scala,C#和F#。
此版本使用Java功能接口,需要Java 8。

Here we give some advice on running microbenchmarks, in particular for managed platforms. Most examples are in Java but the advice applies to any language executed on a managed platform, including Scala, C# and F#. This version uses Java functional interfaces and requires Java 8.

这篇关于排序时间总是与第一次排序不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆