图形表示基准测试
目前正在开发一个程序,可以解决(如果可能的话)任何给定的尺寸从 3X4 到 26x30 的迷宫。我使用 adj 矩阵(稀疏)和 adj 列表来表示该图。我想知道如何输出 DFS 使用一种方法然后使用另一种方法找到解决方案所需的总时间。以编程方式,我怎样才能产生这样的基准?
Currently am developing a program that solves (if possible) any given labyrinth of dimensions from 3X4 to 26x30. I represent the graph using both adj matrix (sparse) and adj list. I would like to know how to output the total time taken by the DFS to find the solution using one and then the other method. Programatically, how could i produce such benchmark?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用于计算各种图形实现的有用表格:
其中
m
是边数,n
是顶点数,d(vertex)
> 是顶点邻接列表中的元素数量。adj 矩阵实现有一个O(n²)
来添加和删除顶点,因为您必须重新分配矩阵。刚刚为一篇文章准备了这个,这就是为什么我准备好了:)
这与基准测试没有直接关系,因为通常您会研究您最需要哪些操作并根据您的需求选择正确的实现,因此这是一种“理论”基准测试,您通过研究什么来完成你将要实施。否则,您可以只测量代码在两种实现中完成整个工作所需的时间并进行比较。
编辑:由于您使用稀疏矩阵实现,操作的复杂性可能会略有变化(主要会变得更糟,因为您正在以内存换取速度)。
编辑2:好吧,现在我知道这是Java,这将很简单:
答案以纳秒为单位,并且不能保证准确性,所以请小心使用这个东西。对多次运行进行平均(并检查方差是否较低,丢弃与平均值相差较大的结果)将使您的结果具有一致性。
An useful table to work out with various graphs implementations:
where
m
is the number of edges,n
is the number of vertices andd(vertex)
is the number of elements in the vertex adjacency list.. adj matrix implementation has anO(n²)
to add and remove vertices because you have to reallocate the matrix..Just prepared this for an article, that why I have it ready :)
This is not directly related to benchmarking, since usually you study which operations you will mostly need and choose the right implementation for your needs, so it's a sort of "theoretical" benchmark that you do by studying what you are going to implement. Otherwise you can just measure time that your code needs to do the whole work with both implementations and compare it.
EDIT: since you use a sparse matrix implementation the complexity of operations could slightly change (mostly getting a little worse, since you are trading memory for speed).
EDIT2: ok, now that I know that this is Java it will be fair simple:
Answer is in nanoseconds and accuracy is not guaranteed, so use this thing carefully. Doing an average of many runs (and checking that variance is low, discarding the result that are more distant from the average) will give coherence to your results.
假设您有合适的方法,这应该相当简单。只需将这两种方法包装在一个计时器中,并重复多次以获得统计意义。
Assuming you have a suitable methods, this should be fairly simple. Just wrap both the methods in a timer, and repeat it many times for statistical significance.