//传入滤波窗口系数和尺寸(nSize*nSize) void CxBitmap::Filter(double* pfFactors, int nSize, BOOL bAve) { double fMedian = 0; int nRead = 0; int nRadius = nSize / 2;//滤波窗口的半径 int nBytePerPixel = bitmapinfoheader.biBitCount / 8; int i, j, k, m, n;
//拷贝边界到临时缓存区 for (i = 0; i < nRadius; ++i) { memcpy(pbyTmpBuffer + i * m_nBytesPerLine, pbyBuffer + i * m_nBytesPerLine, m_nBytesPerLine); memcpy(pbyTmpBuffer + (bitmapinfoheader.biHeight - 1 - i) * m_nBytesPerLine, pbyBuffer + (bitmapinfoheader.biHeight - 1 - i) * m_nBytesPerLine, m_nBytesPerLine); }
for (i = 0; i < bitmapinfoheader.biHeight; ++i) { nRead = i * m_nBytesPerLine; for (j = 0; j < nRadius; ++j) { pbyTmpBuffer[nRead] = pbyBuffer[nRead++]; pbyTmpBuffer[nRead] = pbyBuffer[nRead++]; pbyTmpBuffer[nRead] = pbyBuffer[nRead++]; }
方法二:直接调用StretchDIBits绘制位图。 该函数功能相当强悍,似乎专为从内存数据绘制位图到dc而生。 函数原型如下: int StretchDIBits( HDC hdc, // handle to DC int XDest, // x-coord of destination upper-left corner int YDest, // y-coord of destination upper-left corner int nDestWidth, // width of destination rectangle int nDestHeight, // height of destination rectangle int XSrc, // x-coord of source upper-left corner int YSrc, // y-coord of source upper-left corner int nSrcWidth, // width of source rectangle int nSrcHeight, // height of source rectangle CONST VOID lpBits, // bitmap bits CONST BITMAPINFO lpBitsInfo, // bitmap data UINT iUsage, // usage options DWORD dwRop // raster operation code ); 使用也相当简单,调用
该函数原型如下, BOOL SetWindowPos( HWND hWnd, // handle to window HWND hWndInsertAfter, // placement-order handle int X, // horizontal position int Y, // vertical position int cx, // width int cy, // height UINT uFlags // window-positioning options );
例子二,预先计算某些结果以优化后面的计算。 比如,你需要将一个字符串转换为大写的,你是对字符串循环一次,循环内部调用toupper了,还是预先计算出toupper的结果,以后每次查表了。 优化后的代码应该是类似于这样的。 for (char p = header; p; ++p) p = uppercaseTable[p]; 你所需要做的就是在初始化时候构造出uppercaseTable这张大小为256的表,如果你需要经常做大写转换无疑能不错的提高性能。
例子七,恰当利用系统的缓存机制。 比如,下面结构体的定义。 struct X { int a; char b[4096]; int c;}; 应该改成struct X { int a; int c;char b[4096]; }; 原因是cpu缓存可能就128个字节,那么加载a的时候可能一次缓存了连在一起的128个字节。由于a和c之间距离比较远,那么c就不在缓存行里面了。如果,你需要再访问c就会造成缓存失败,就需要重新加载缓存。这种降低缓存命中率的事情确实不怎么好。但是,你也不知道编译器会不会调整内存结果,把a和c默认连在一起了。我觉得还是不要做这种假设吧,这完全是改变编程者的意图了。
void TestBigIntCreate(int n) { printf("TestBigIntCreate:%d\n", n); clock_t beg = clock(); for (int i = 0; i < n; ++i) { BigInt a = i; BigInt b = i + 1; BigInt c = i + 2; BigInt d = i + 3; } clock_t end = clock(); printf("use %f second(s).\n", 1.0 * (end - beg) / CLOCKS_PER_SEC); }
void TestRCBigIntCreate(int n) { printf("TestRCBigIntCreate:%d\n", n); clock_t beg = clock(); for (int i = 0; i < n; ++i) { RCBigInt a = i; RCBigInt b = i + 1; RCBigInt c = i + 2; RCBigInt d = i + 3; } clock_t end = clock(); printf("use %f second(s).\n", 1.0 * (end - beg) / CLOCKS_PER_SEC); }
void TestBigIntAssign(int n) { printf("TestBigIntAssign:%d\n", n); BigInt a, b, c, d; clock_t beg = clock(); for (int i = 0; i < n; ++i) { a = b = c = d; } clock_t end = clock(); printf("use %f second(s).\n", 1.0 * (end - beg) / CLOCKS_PER_SEC); }
void TestRCBigIntAssign(int n) { printf("TestRCBigIntAssign:%d\n", n); RCBigInt a, b, c, d; clock_t beg = clock(); for (int i = 0; i < n; ++i) { a = b = c = d; } clock_t end = clock(); printf("use %f second(s).\n", 1.0 * (end - beg) / CLOCKS_PER_SEC); }
void TestBigIntCreate(int n) { printf("TestBigIntCreate:%d\n", n); clock_t beg = clock(); for (int i = 0; i < n; ++i) { BigInt a = i; BigInt b = i + 1; BigInt c = i + 2; BigInt d = i + 3; } clock_t end = clock(); printf("use %f second(s).\n", 1.0 * (end - beg) / CLOCKS_PER_SEC); }
void TestRCBigIntCreate(int n) { printf("TestRCBigIntCreate:%d\n", n); clock_t beg = clock(); for (int i = 0; i < n; ++i) { RCBigInt a = i; RCBigInt b = i + 1; RCBigInt c = i + 2; RCBigInt d = i + 3; } clock_t end = clock(); printf("use %f second(s).\n", 1.0 * (end - beg) / CLOCKS_PER_SEC); }
void TestBigIntAssign(int n) { printf("TestBigIntAssign:%d\n", n); BigInt a, b, c, d; clock_t beg = clock(); for (int i = 0; i < n; ++i) { a = b = c = d; } clock_t end = clock(); printf("use %f second(s).\n", 1.0 * (end - beg) / CLOCKS_PER_SEC); }
void TestRCBigIntAssign(int n) { printf("TestRCBigIntAssign:%d\n", n); RCBigInt a, b, c, d; clock_t beg = clock(); for (int i = 0; i < n; ++i) { a = b = c = d; } clock_t end = clock(); printf("use %f second(s).\n", 1.0 * (end - beg) / CLOCKS_PER_SEC); }
void TestBigIntCreate(int n) { printf("TestBigIntCreate:%d\n", n); clock_t beg = clock(); for (int i = 0; i < n; ++i) { BigInt a = i; BigInt b = i + 1; BigInt c = i + 2; BigInt d = i + 3; } clock_t end = clock(); printf("use %f second(s).\n", 1.0 * (end - beg) / CLOCKS_PER_SEC); }
void TestRCBigIntCreate(int n) { printf("TestRCBigIntCreate:%d\n", n); clock_t beg = clock(); for (int i = 0; i < n; ++i) { RCBigInt a = i; RCBigInt b = i + 1; RCBigInt c = i + 2; RCBigInt d = i + 3; } clock_t end = clock(); printf("use %f second(s).\n", 1.0 * (end - beg) / CLOCKS_PER_SEC); }
void TestBigIntAssign(int n) { printf("TestBigIntAssign:%d\n", n); BigInt a, b, c, d; clock_t beg = clock(); for (int i = 0; i < n; ++i) { a = b = c = d; } clock_t end = clock(); printf("use %f second(s).\n", 1.0 * (end - beg) / CLOCKS_PER_SEC); }
void TestRCBigIntAssign(int n) { printf("TestRCBigIntAssign:%d\n", n); RCBigInt a, b, c, d; clock_t beg = clock(); for (int i = 0; i < n; ++i) { a = b = c = d; } clock_t end = clock(); printf("use %f second(s).\n", 1.0 * (end - beg) / CLOCKS_PER_SEC); }
void TestBigIntCreate(int n) { printf("TestBigIntCreate:%d\n", n); clock_t beg = clock(); for (int i = 0; i < n; ++i) { BigInt a = i; BigInt b = i + 1; BigInt c = i + 2; BigInt d = i + 3; } clock_t end = clock(); printf("use %f second(s).\n", 1.0 * (end - beg) / CLOCKS_PER_SEC); }
void TestRCBigIntCreate(int n) { printf("TestRCBigIntCreate:%d\n", n); clock_t beg = clock(); for (int i = 0; i < n; ++i) { RCBigInt a = i; RCBigInt b = i + 1; RCBigInt c = i + 2; RCBigInt d = i + 3; } clock_t end = clock(); printf("use %f second(s).\n", 1.0 * (end - beg) / CLOCKS_PER_SEC); }
void TestBigIntAssign(int n) { printf("TestBigIntAssign:%d\n", n); BigInt a, b, c, d; clock_t beg = clock(); for (int i = 0; i < n; ++i) { a = b = c = d; } clock_t end = clock(); printf("use %f second(s).\n", 1.0 * (end - beg) / CLOCKS_PER_SEC); }
void TestRCBigIntAssign(int n) { printf("TestRCBigIntAssign:%d\n", n); RCBigInt a, b, c, d; clock_t beg = clock(); for (int i = 0; i < n; ++i) { a = b = c = d; } clock_t end = clock(); printf("use %f second(s).\n", 1.0 * (end - beg) / CLOCKS_PER_SEC); }