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

相關文章
相關標籤/搜索