1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
   | #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <ostream>
 
#include "windows.h" // oué bon je sais... (mais j'ai pas boost d'installé là et je suis pressé :p )
 
using namespace std;
 
class PerfTimer
{
public:
	PerfTimer();
	~PerfTimer();
 
	static bool Init()
	{
		LARGE_INTEGER proc_freq;
 
		if ( !QueryPerformanceFrequency( &proc_freq ) ) 
			return false;
 
		frequency = 1.0 / proc_freq.QuadPart;
		return true;
	}
 
	void	Start() { QueryPerformanceCounter( &start ); }
 
	double	Stop()
	{
		QueryPerformanceCounter( &stop );
		return ( ( stop.QuadPart - start.QuadPart ) * frequency );
	}
 
	void	StopAndPrint( const std::string & message, std::ostream & ostr );
	{
		double duracion = Stop();
		ostr << message << duracion << " ms" << std::endl;
	}
 
private:
	static double	frequency;
	LARGE_INTEGER	start;
	LARGE_INTEGER	stop;
};
 
double PerfTimer::frequency = 0;
 
struct MyGenerate
{
	MyGenerate():counter(0){}
	int operator () () { return counter++; }
private:
	int counter;
};
 
int main()
{
	if ( !PerfTimer::Init() )
	{
		cout << "error: problema en la inicializacion de PerfTimer" << endl;
		return EXIT_FAILURE;
	}
 
	PerfTimer pt;
	vector<int> v( 500000 );
 
	pt.Start();
	generate( v.begin(), v.end(), MyGenerate() );
	pt.StopAndPrint( "generate: ", cout );
 
	pt.Start();
	v.clear();
	pt.StopAndPrint( "clear: ", cout );
 
	cout << "cap: " << v.capacity() << endl;
 
	vector<int> v1( 500000 );
	generate( v1.begin(), v1.end(), MyGenerate() );
 
	pt.Start();
	vector<int>().swap( v1 );
	pt.StopAndPrint( "swap: ", cout );
 
	cout << "cap: " << v1.capacity() << endl;
 
	cout << "end" << endl;
	cin.get();
	return EXIT_SUCCESS;
} | 
Partager