7个寻路算法 效率测试

 1 void main()
 2 {
 3     Path *path;
 4     time_t t1,t2;
 5     int i, times = 10000;
 6 
 7     t1 = clock();
 8     for(i = 0; i < times; ++i)
 9     {
10         AStar::find(_size, _start, _end, visit, &path);
11     }
12     t2 = clock();cout<<"A* "<<t2-t1<<endl;
13 
14     t1 = clock();
15     for(i = 0; i < times; ++i)
16     {
17         BStar::find(_size, _start, _end, visit, &path);
18     }
19     t2 = clock();cout<<"B* "<<t2-t1<<endl;
20 
21     t1 = clock();
22     for(i = 0; i < times; ++i)
23     {
24         BFS::find(_size, _start, _end, visit, &path);
25     }
26     t2 = clock();cout<<"BFS "<<t2-t1<<endl;
27 
28     t1 = clock();
29     for(i = 0; i < times; ++i)
30     {
31         Dijkstra::find(_size, _start, _end, visit, &path);
32     }
33     t2 = clock();cout<<"Dijkstra "<<t2-t1<<endl;
34 
35     t1 = clock();
36     Dijkstra dij;
37     dij.init(_size, _end, visit);
38     for(i = 0; i < times; ++i)
39     {
40         dij.findPath(_start, &path);
41     }
42     t2 = clock();cout<<"Dijkstra1 "<<t2-t1<<endl;
43 
44     t1 = clock();
45     for(i = 0; i < times; ++i)
46     {
47         SPFA::find(_size, _start, _end, visit, &path);
48     }
49     t2 = clock();cout<<"SPFA "<<t2-t1<<endl;
50     
51     t1 = clock();
52     SPFA spfa;
53     spfa.init(_size, _end, visit);
54     for(i = 0; i < times; ++i)
55     {
56         spfa.findPath(_start, &path);
57     }
58     t2 = clock();cout<<"Dijkstra1 "<<t2-t1<<endl;
59 
60     t1 = clock();
61     Floyd::find(_size, _start, _end, visit, &path);
62     t2 = clock();cout<<"Floyd "<<t2-t1<<endl;
63 
64     printPath(path);
65 }

地图还算简单我寻路10000次。算法

别怪我吐槽A*算法,竟然比SPFA还慢,人家SPFA但是把全部到终点的路径都算好了哦。函数

 

 

这里我要说下Dijkstra为何都3156了那是个人优先队列太垃圾了,测试

PriorityQueue<Info*, InfoCmp> visit;
//std::priority_queue<Info*, std::vector<Info*>, InfoCmp> visit;spa

我用了我本身的PriorityQueue,若是用std的priority_queue只要1375ms。code

那你问我为何不用std的priority_queue,那是我在作跨平台是运行错误了。不得已本身写的。blog

你能够本身测试,注意我本身的PriorityQueue和std的比较函数是反的,发现路径不对把下面的比较函数颠倒下就能够了。队列

    struct InfoCmp
    {
        bool operator () (const Info *p1, const Info *p2)
        {
            return p1->dis > p2->dis;
        }
    };it

 

 

你看你看到Floyd怎么才453ms,那是我只寻了一次,你也知道它有多慢了。io

那你以为Floyd是否是就没用了?class

相关文章
相关标签/搜索