【问题标题】:Process im trying to calculate running time is executing in 0 nanoseconds. how?试图计算运行时间的进程在 0 纳秒内执行。如何?
【发布时间】:2019-08-25 00:54:42
【问题描述】:

我正在尝试计算我的算法性能。基本上我的算法解决了集合操作,如联合、析取、交集等。我的算法可以在 O(logn) 中完成这些操作,以与类似的算法进行比较计算运行时间我看到其他算法中的一些操作实际上需要 0(零)纳秒来完成。通过操作,我的意思是找到两个集合的并集,每个集合中有 10000 个元素。这怎么可能?你可以在Github 上看到我的项目。 我计算运行时间的部分在测试包中

我尝试使用 Jprofilier 来确保它全部在一个线程上运行

我尝试在 to 时间间隔之间进行调试,以确保它不会忽略计算并找到正确的结果

static Duration IntersectDocumentsTime(AlgorithmInterface algorithm)
{
        Instant start = Instant.now(); // Time before calulation i tryed to put breakpoint here
        algorithm.IntersectDocuments(); // returns Term[] result of elements after union operation
        Instant end = Instant.now(); // Time after calulation
        return Duration.between(start,end); // i put a breakpoint here to see if IntersectDocuments() result is correct and actually calculated
}

这就是我打印结果的方式

for (AlgorithmInterface al: Algorithms)
        {
            System.out.println("------------------------------------------------------------------------------");
            System.out.println("Algorithm: "+al.getClass().toString()+"\tOperation: Union\nTime: "
                    +OperationsInterface.AddDocumentsTime(al).toNanos()+"\tseconds");
            System.out.println("Algorithm: "+al.getClass().toString()+"\tOperation: Disjuction\nTime: "
                    +OperationsInterface.DisjointDocumentsTime(al).toNanos()+"\tseconds");
            System.out.println("Algorithm: "+al.getClass().toString()+"\tOperation: Intersection\nTime: "
                    +OperationsInterface.IntersectDocumentsTime(al).toNanos()+"\tseconds");
            System.out.println("Algorithm: "+al.getClass().toString()+"\tOperation: Subtraction\nTime: "
                    +OperationsInterface.SubtractDocumentsTime(al).toNanos()+"\tseconds");
            System.out.println("Algorithm: "+al.getClass().toString()+"\tOperation: Find\nTime: "
                    +OperationsInterface.ContainsTermTime(al,new Term("A")).toNanos()+"\tseconds");
        }
System.out.println("------------------------------------------------------------------------------");

结果如下所示


算法:类 Algorithms.FNA.FNA 操作:联合 时间:1851133600秒 算法:类 Algorithms.FNA.FNA 运算:析取 时间:1799607700秒 算法:类 Algorithms.FNA.FNA 运算:交集 时间:291703600 秒 算法:类 Algorithms.FNA.FNA 运算:减法 时间:1022775100 秒 算法:类 Algorithms.FNA.FNA 操作:查找 时间:1319100秒


算法:类 Algorithms.Primitive.Primitive 操作:联合 时间:81257800秒 算法:类 Algorithms.Primitive.Primitive 运算:析取 时间:85717600秒 算法:类 Algorithms.Primitive.Primitive 运算:交集 时间:0秒 算法:类 Algorithms.Primitive.Primitive 运算:减法 时间:66472900 秒 算法:类 Algorithms.Primitive.Primitive 操作:查找 时间:0秒


算法:类 Algorithms.BloomsFilter.BloomsFilter 操作:联合 时间:998900秒 算法:类 Algorithms.BloomsFilter.BloomsFilter 操作:析取 时间:0秒 算法:类 Algorithms.BloomsFilter.BloomsFilter 操作:交集 时间:503800秒 算法:类 Algorithms.BloomsFilter.BloomsFilter 操作:减法 时间:0秒 算法:类 Algorithms.BloomsFilter.BloomsFilter 操作:查找 时间:1312900秒


算法:类 Algorithms.SortedList.SortedList 操作:联合 时间:0秒 算法:类 Algorithms.SortedList.SortedList 操作:析取 时间:3721800秒 算法:类 Algorithms.SortedList.SortedList 操作:交集 时间:0秒 算法:类 Algorithms.SortedList.SortedList 操作:减法 时间:810500秒 算法:类 Algorithms.SortedList.SortedList 操作:查找 时间:1173200秒


【问题讨论】:

标签: java performance time time-complexity


【解决方案1】:

Instant.now() 将使用当前时间(以毫秒为单位)。深入研究代码,您可以看到它使用了System.currentTimeMillis()。 除了使用上述方法,您可以使用以下代码,其中我使用了纳秒时间。

static long IntersectDocumentsTime(AlgorithmInterface algorithm) {
    long start = System.nanoTime();
    algorithm.IntersectDocuments();
    long end = System.nanoTime();
    long durationInNanos = end - start;
    return durationInNanos;
}

【讨论】:

  • 原因是Instant.now() 使用毫秒。但是在这里我使用了纳秒并得到了它的不同。
【解决方案2】:

不要自己编写微基准测试,使用JMH 框架。这将确保您避免常见的陷阱,例如分层编译。

在您的代码中:

Instant start = Instant.now(); 
algorithm.IntersectDocuments();
Instant end = Instant.now();
return Duration.between(start,end);

如果没有副作用,algorithm.IntersectDocuments() 可以被优化掉。

请注意Instant.now() 在幕后使用System.currentTimeMillis(),即not suitable for calculating elapsed time

【讨论】:

  • 该方法有副作用吗?它可以预先计算并内联到无操作中吗?这很可能是来自使用System.currentTimeMillis()Instant.now() 的时钟漂移,但这是一个猜测。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-29
  • 1970-01-01
  • 1970-01-01
  • 2014-04-23
  • 2016-07-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多