c_cpp STL抽象测试

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp STL抽象测试相关的知识,希望对你有一定的参考价值。

#include <windows.h>

#include <stdio.h>
#include <math.h>

#include <vector>

using namespace std;

__declspec(naked)
	unsigned __int64 __cdecl rdtsc(void) {
		__asm {
			rdtsc
				ret
		}
}

#define NUM_ITERATIONS 10000
void PrepEnvironment(void) {
	// Move everything to one CPU, we're going
	// to be using its clock counter
	SetThreadAffinityMask(GetCurrentThread(), 1);

	// Commit all the memory.
	void *p = malloc(sizeof(int) * 4 * NUM_ITERATIONS);
	memset(p, 0, sizeof(int) * 4 * NUM_ITERATIONS);
	free(p);
}

//int __cdecl comparator(const void *p1,
//                       const void *p2) {
//  const unsigned int *pi1 =
//      (const unsigned int*)p1;
//  const unsigned int *pi2 =
//      (const unsigned int*)p2;
//  return (int)(*pi1 - *pi2);
//}
//
//#define BUCKETS 20
void DumpStats(unsigned int *clocks,
			   unsigned int cptr,
			   unsigned int iters) {
				   //qsort(clocks, cptr,
				   //    sizeof(unsigned int), comparator);

				   /* int buckets[BUCKETS];
				   memset(buckets, 0, sizeof(buckets));*/

				   double average = 0;
				   double log2 = log(2.0);

				   for (unsigned int i = 0 ; i < cptr; ++i) {
					   average += (double)clocks[i];
					   /*   int ndx = (int)(log((double)clocks[i]
					   / 1000.0) / log2);
					   if (ndx < 0)
					   buckets[0]++;
					   else if (ndx < BUCKETS-1)
					   buckets[ndx + 1]++;
					   else
					   buckets[BUCKETS-1]++;*/
				   }

				   average /= cptr;

				   /*printf("Median: %d\n",
				   (int)clocks[cptr / 2]);
				   printf("Median, CPI: %d\n",
				   clocks[cptr / 2] / iters);*/
				   printf("Average: %d\n", (int)average);
				   printf("Average, CPI: %d\n",
					   (int)(average / iters));

				   //printf("Histogram:\n");
				   //int prev = 0;
				   //int curr = 1000;
				   //for (int i = 0; i < BUCKETS - 1; ++i) {
				   //  printf("%d - %d: %d\n", prev, curr,
				   //      buckets[i]);
				   //  prev = curr;
				   //  curr *= 2;
				   //}

				   //printf(" > %d: %d\n", prev,
				   //    buckets[BUCKETS - 1]);
}

#define NUM_SAMPLES 10000
int wmain() {
	PrepEnvironment();

	static unsigned int clocks[NUM_SAMPLES];
	for (int k = 0; k < 4; ++k) {
		unsigned int cptr = 0;

		switch(k) {
		case 0:
			printf("STL re-alloc\n");
			for (int i = 0; i < NUM_SAMPLES; ++i) {
				vector<int> work;
				unsigned __int64 begin = rdtsc();
				for (int j = 0; j < NUM_ITERATIONS; ++j)
					work.emplace_back(i);

				unsigned __int64 end = rdtsc();
				clocks[cptr++] =
					(unsigned int)(end - begin);
			}
			break;
		case 1:
			printf("STL pre-alloc\n");
			for (size_t i = 0; i < NUM_SAMPLES; ++i) {
				vector<int> work(NUM_ITERATIONS);
				work.reserve(NUM_ITERATIONS);
				unsigned __int64 begin = rdtsc();
				for (size_t j = 0; j < NUM_ITERATIONS; ++j)
					work.emplace_back(i);
				unsigned __int64 end = rdtsc();
				clocks[cptr++] =
					(unsigned int)(end - begin);
			}
			break;
		case 2:
			printf("C re-alloc\n");
			for (int i = 0; i < NUM_SAMPLES; ++i) {
				int size = 20;
				int *work = (int *)malloc(size * sizeof(int));
				unsigned __int64 begin = rdtsc();
				for (int j = 0; j < NUM_ITERATIONS; ++j) {
					if (j >= size) {
						// Match STL vector allocation algorithm
						size = size + size / 2;
						work = (int *)realloc(work, size
							* sizeof(int));
					}
					work[j] = j;
				}
				unsigned __int64 end = rdtsc();
				clocks[cptr++] =
					(unsigned int)(end - begin);
				free(work);
			}
			break;
		case 3:
			printf("C pre-alloc\n");
			for (int i = 0; i < NUM_SAMPLES; ++i) {
				int *work = (int *)malloc(
					NUM_ITERATIONS * sizeof(int));
				int size = NUM_ITERATIONS;
				unsigned __int64 begin = rdtsc();
				for (int j = 0; j < NUM_ITERATIONS; ++j)
				{
					if (j >= size) {
						// Match STL vector allocation algorithm
						size = size + size / 2;
						work = (int *)realloc(work, size
							* sizeof(int));
					}
					work[j] = j;
				}
				unsigned __int64 end = rdtsc();
				clocks[cptr++] =
					(unsigned int)(end - begin);
				free(work);
			}
			break;
		}

		DumpStats(clocks, cptr, NUM_ITERATIONS);
		printf("\n\n");
	}

	return 0;
}

以上是关于c_cpp STL抽象测试的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp STL_sort_rank

c_cpp StringsAPI用于STL字符串

c_cpp C ++抽象工厂

c_cpp C ++抽象工厂

徒手写的AVL竟然比STL中的红黑树效率更高?✨

c_cpp C中的抽象节点eval