C语言 位图法存储大量数据
2010年08月13日
题目来源于《编程珠玑》里面的第一章。(大概有10M(1024*1024*10)个非重复数据需要排序)
关键词:高精度定时器,大量非重复数据排序,内存排序,BIT位操作 #include #include #include #include #include #include #include #ifndef TRUE #define TRUE 1 #endif #ifndef FALSE #define FALSE 0 #endif // 高精度计时器所需变量 LARGE_INTEGER m_Frequency; LARGE_INTEGER m_StartCount; LARGE_INTEGER m_EndCount; // 高精度计时器开始计时 void TimeCounterStart() { assert(QueryPerformanceFrequency(&m_Frequency)); assert(QueryPerformanceCounter(&m_StartCount)); } // 高精度计时器结束计时 double TimeCounterEnd() { double dTotalTimeValue; assert(QueryPerformanceCounter(&m_EndCount)); dTotalTimeValue = 1000.00 * ((double)(m_EndCount.QuadPart - m_StartCount.QuadPart) / (double)m_Frequency.QuadPart); m_Frequency.QuadPart = 0; m_StartCount.QuadPart = 0; m_EndCount.QuadPart = 0; // 这里返回的是毫秒级的值 return dTotalTimeValue; } // Note: nOffset = [0, ..., n] // 将pPointer当作char数组来处理 int SetBit(const void *pPointer, unsigned int nOffset, unsigned char nValue) { int nReurnValue = TRUE; unsigned int nBytesOffset = nOffset / 8; unsigned int nBitOffset = nOffset % 8; unsigned char *pTmpPointer = ((unsigned char *)pPointer) + nBytesOffset; unsigned char number = 1 > 7; if (number == nValue) { nReurnValue = TRUE; } else { nReurnValue = FALSE; } return nReurnValue; } // 利用位图法来实现大量数字的存储和查询 void BitGraphMethodTest() { unsigned int i; unsigned int nMemorySize = 1024 * 1024 * 10 * sizeof(char); unsigned int nBitSize = nMemorySize * 8; char *newMemory = (char *)malloc(nMemorySize); double dTotalTime = 0.0; if (!newMemory) { printf("malloc memory failed\n"); return; } memset(newMemory, 0, nMemorySize); // 计时器开始 TimeCounterStart(); for (i = 0; i fms\n", dTotalTime); printf("All Location SetBit Success. BitSize = %d\n", nBitSize); GetBit(newMemory, 1, 1) ? printf("SetBit Success.\n") : printf("SetBit Failed.\n");; free(newMemory); newMemory = NULL; return; } /* 结果如下,存取速度相当的快. 位图法实现大量数据存储 --> Total Time: 3402.740174ms All Location SetBit Success. BitSize = 83886080 SetBit Success. --> 位图法实现大量数据存储 */