list的sort和loop比较

本文通过实验证明,在不同数据规模下,使用C++中的sort函数相较于循环进行排序,效能表现显著提升。通过对10000、100000、1000000个元素的比较,发现sort函数的平均执行时间分别是循环的13倍、31倍、36倍,揭示了高效排序算法在实际应用中的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

比较了一下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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值