list的sort和loop比较

来源:互联网 发布:win10下安装linux 编辑:程序博客网 时间:2024/05/15 12:40

比较了一下sort和循环的效率,结果如下

 

10000 == sort 203, loop 15            13倍
100000 == sort 2469, loop 78        31倍
1000000 == sort 29015, loop 797  36倍

 

// tlist.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <Windows.h>
#include <list>
int _tmain(int argc, _TCHAR* argv[])
{
 using namespace std;
 list< int > tlist;
 list <int>::iterator c1_Iter;
/*
10000 == sort 203, loop 15 13
100000 == sort 2469, loop 78 31
1000000 == sort 29015, loop 797 36
*/
 for (int i=0; i < 10000; i++)
 {
  tlist.push_front(rand());
 }

 int tim = GetTickCount();
 tlist.sort();
 printf("sort %d/r/n", GetTickCount() - tim);

 tim = GetTickCount();
 for ( c1_Iter = tlist.begin( ); c1_Iter != tlist.end( ); c1_Iter++ )
 {
  if (c1_Iter == tlist.begin( ))
  {
   printf("%d/r/n", *c1_Iter);
  }
 }
 printf("loop %d/r/n", GetTickCount() - tim);

 getc(stdin);
 return 0;
}